Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parity of noise model conversion args #578

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
"""Loads a PennyLane `NoiseModel <https://docs.pennylane.ai/en/stable/code/api/pennylane.NoiseModel.html>`_
from a Qiskit `noise model <https://qiskit.github.io/qiskit-aer/stubs/qiskit_aer.noise.NoiseModel.html>`_.

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.
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
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:
pennylane.NoiseModel: An equivalent noise model constructed in PennyLane
Expand Down Expand Up @@ -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:
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading