From f220027aa8f3278bf75fab37e69b141d848094bc Mon Sep 17 00:00:00 2001 From: obliviateandsurrender Date: Mon, 29 Jul 2024 10:48:17 -0400 Subject: [PATCH 1/5] args parity --- pennylane_qiskit/converter.py | 24 ++++++++++++------------ tests/test_converter.py | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pennylane_qiskit/converter.py b/pennylane_qiskit/converter.py index adc753b5..abf2d2a6 100644 --- a/pennylane_qiskit/converter.py +++ b/pennylane_qiskit/converter.py @@ -1225,19 +1225,19 @@ def _expr_eval_clvals(clbits, clvals, expr_func, bitwise=False): return condition_res -def load_noise_model(noise_model, **kwargs) -> qml.NoiseModel: +def load_noise_model( + noise_model, verbose: bool = False, decimal_places: Union[int, None] = None +) -> qml.NoiseModel: """Loads a PennyLane `NoiseModel `_ from a Qiskit `noise model `_. Args: noise_model (qiskit_aer.noise.NoiseModel): a Qiskit noise model object - kwargs: optional keyword arguments for the conversion of the noise model - - Keyword Arguments: - verbose (bool): show a complete list of Kraus matrices for ``qml.QubitChannel`` instead of - number of Kraus matrices and the number of qubits they act on. The default is ``False`` - decimal_places (int): number of decimal places to round the Kraus matrices when they are - being displayed for each ``qml.QubitChannel`` with ``verbose=False``. + verbose (bool): when printing a ``NoiseModel``, a complete list of Kraus matrices for each ``qml.QubitChannel`` + is displayed with ``verbose=True``. By default, ``verbose=False`` and only the number of Kraus matrices and + the number of qubits they act on is displayed for brevity. + decimal_places (int): number of decimal places to round the elements of Kraus matrices when they are being + displayed for each ``qml.QubitChannel`` when ``verbose=True``. Returns: pennylane.NoiseModel: An equivalent noise model constructed in PennyLane @@ -1281,14 +1281,14 @@ def load_noise_model(noise_model, **kwargs) -> qml.NoiseModel: fcond = reduce(lambda cond1, cond2: cond1 | cond2, conditions) noise = qml.noise.partial_wires(error) - if isinstance(error, qml.QubitChannel) and not kwargs.get("verbose", False): + if isinstance(error, qml.QubitChannel) and not verbose: kraus_shape = qml.math.shape(error.data) num_kraus, num_wires = kraus_shape[0], int(np.log2(kraus_shape[1])) noise = _rename(f"QubitChannel(num_kraus={num_kraus}, num_wires={num_wires})")(noise) - if isinstance(error, qml.QubitChannel) and kwargs.get("verbose", False): - if (decimals := kwargs.get("decimal_places", None)) is not None: - kraus_matrices = list(np.round(error.data, decimals=decimals)) + if isinstance(error, qml.QubitChannel) and verbose: + if decimal_places is not None: + kraus_matrices = list(np.round(error.data, decimals=decimal_places)) noise = _rename(f"QubitChannel(Klist={kraus_matrices})")(noise) model_map[fcond] = noise diff --git a/tests/test_converter.py b/tests/test_converter.py index a268e897..3e0dcdf3 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -2662,8 +2662,8 @@ def test_build_noise_model(self): "verbose, decimal", [(True, 8), (False, None)], ) - def test_build_noise_model_with_kwargs(self, verbose, decimal): - """Tests that ``load_quantum_noise`` constructs a correct PennyLane NoiseModel with kwargs""" + def test_build_noise_model_with_args(self, verbose, decimal): + """Tests that ``load_quantum_noise`` constructs a correct PennyLane NoiseModel with args""" from qiskit_aer import noise error_1 = noise.depolarizing_error(0.001, 1) From 44b68b9f57f48e541f790e267310b4db7a072e2f Mon Sep 17 00:00:00 2001 From: obliviateandsurrender Date: Mon, 29 Jul 2024 10:50:24 -0400 Subject: [PATCH 2/5] type hint --- pennylane_qiskit/converter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_qiskit/converter.py b/pennylane_qiskit/converter.py index abf2d2a6..f1b7cc35 100644 --- a/pennylane_qiskit/converter.py +++ b/pennylane_qiskit/converter.py @@ -1236,7 +1236,7 @@ def load_noise_model( verbose (bool): when printing a ``NoiseModel``, a complete list of Kraus matrices for each ``qml.QubitChannel`` is displayed with ``verbose=True``. By default, ``verbose=False`` and only the number of Kraus matrices and the number of qubits they act on is displayed for brevity. - decimal_places (int): number of decimal places to round the elements of Kraus matrices when they are being + decimal_places (int | None): number of decimal places to round the elements of Kraus matrices when they are being displayed for each ``qml.QubitChannel`` when ``verbose=True``. Returns: From 03bc565199ccc9823ff47bfdeb3845c8ee9b55f5 Mon Sep 17 00:00:00 2001 From: obliviateandsurrender Date: Mon, 29 Jul 2024 12:54:52 -0400 Subject: [PATCH 3/5] conditional flow --- pennylane_qiskit/converter.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pennylane_qiskit/converter.py b/pennylane_qiskit/converter.py index f1b7cc35..c2dfe9d0 100644 --- a/pennylane_qiskit/converter.py +++ b/pennylane_qiskit/converter.py @@ -1281,13 +1281,12 @@ def load_noise_model( fcond = reduce(lambda cond1, cond2: cond1 | cond2, conditions) noise = qml.noise.partial_wires(error) - if isinstance(error, qml.QubitChannel) and not verbose: - kraus_shape = qml.math.shape(error.data) - num_kraus, num_wires = kraus_shape[0], int(np.log2(kraus_shape[1])) - noise = _rename(f"QubitChannel(num_kraus={num_kraus}, num_wires={num_wires})")(noise) - - if isinstance(error, qml.QubitChannel) and verbose: - if decimal_places is not None: + if isinstance(error, qml.QubitChannel): + if not verbose: + kraus_shape = qml.math.shape(error.data) + n_kraus, n_wires = kraus_shape[0], int(np.log2(kraus_shape[1])) + noise = _rename(f"QubitChannel(num_kraus={n_kraus}, num_wires={n_wires})")(noise) + elif verbose and decimal_places is not None: kraus_matrices = list(np.round(error.data, decimals=decimal_places)) noise = _rename(f"QubitChannel(Klist={kraus_matrices})")(noise) From cb05a045b53f00ff921a33ebb81bf62f9ac31178 Mon Sep 17 00:00:00 2001 From: obliviateandsurrender Date: Mon, 29 Jul 2024 13:03:20 -0400 Subject: [PATCH 4/5] add `changelog` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b18dadaf..c4199ec6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Added support for converting Qiskit noise models to PennyLane ``NoiseModels`` using ``load_noise_model``. [(#577)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/577) + [(#578)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/578) * Qiskit Sessions can now be used for the ``qiskit.remote`` device with the ``qiskit_session`` context manager. From bc5bbe1f388a720e6f13595d6ea07eba1a3a6ee5 Mon Sep 17 00:00:00 2001 From: obliviateandsurrender Date: Tue, 30 Jul 2024 09:49:40 -0400 Subject: [PATCH 5/5] tweak test --- tests/test_converter.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_converter.py b/tests/test_converter.py index 3e0dcdf3..a0143112 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -2610,11 +2610,11 @@ def test_build_noise_model(self): pl_model_map = { op_in("Identity") & wires_in(0): qml.ThermalRelaxationError( - pe=0.0, t1=26981.9403362283, t2=26034.6676428009, tg=1.0, wires=AnyWires + 0.0, 26981.9403362283, 26034.6676428009, 1.0, wires=AnyWires ), op_in("Identity") & wires_in(1): qml.ThermalRelaxationError( - pe=0.0, t1=30732.034088541, t2=28335.6514829973, tg=1.0, wires=AnyWires + 0.0, 30732.034088541, 28335.6514829973, 1.0, wires=AnyWires ), (op_in("U1") & wires_in(0)) | (op_in("U1") & wires_in(1)): qml.DepolarizingChannel( @@ -2622,19 +2622,19 @@ def test_build_noise_model(self): ), op_in("U2") & wires_in(0): qml.ThermalRelaxationError( - pe=0.4998455776, t1=7.8227384666, t2=7.8226559459, tg=1.0, wires=AnyWires + 0.4998455776, 7.8227384666, 7.8226559459, 1.0, wires=AnyWires ), op_in("U2") & wires_in(1): qml.ThermalRelaxationError( - pe=0.4998644198, t1=7.8227957211, t2=7.8226273195, tg=1.0, wires=AnyWires + 0.4998644198, 7.8227957211, 7.8226273195, 1.0, wires=AnyWires ), op_in("U3") & wires_in(0): qml.ThermalRelaxationError( - pe=0.4996911588, t1=7.8227934813, t2=7.8226284393, tg=1.0, wires=AnyWires + 0.4996911588, 7.8227934813, 7.8226284393, 1.0, wires=AnyWires ), op_in("U3") & wires_in(1): qml.ThermalRelaxationError( - pe=0.4997288404, t1=7.8229079927, t2=7.8225711871, tg=1.0, wires=AnyWires + 0.4997288404, 7.8229079927, 7.8225711871, 1.0, wires=AnyWires ), op_in("CNOT") & wires_in([0, 1]): qml.QubitChannel(