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

Add conversion support for Qiskit's NoiseModels #569

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7ea085a
initial helper functions
obliviateandsurrender Jul 5, 2024
aaa3ae9
add conversion and coellection
obliviateandsurrender Jul 5, 2024
a31c8e4
add options
obliviateandsurrender Jul 8, 2024
a23a8e3
add documentation
obliviateandsurrender Jul 8, 2024
10768b9
tweak docs
obliviateandsurrender Jul 8, 2024
0badd30
minor tweaks
obliviateandsurrender Jul 8, 2024
717f78f
add tests
obliviateandsurrender Jul 8, 2024
974d70f
add tests
obliviateandsurrender Jul 9, 2024
7d97b96
add missing test
obliviateandsurrender Jul 9, 2024
fc73f7a
minor tweak
obliviateandsurrender Jul 9, 2024
caf5da2
update pl sphinx
obliviateandsurrender Jul 9, 2024
d936023
minor test tweak
obliviateandsurrender Jul 9, 2024
c30f35a
Merge branch 'master' into noise-model-convert
obliviateandsurrender Jul 9, 2024
46d60f1
missing test
obliviateandsurrender Jul 9, 2024
7035492
Merge branch 'noise-model-convert' of https://github.com/PennyLaneAI/…
obliviateandsurrender Jul 9, 2024
3eb56e0
tweak
obliviateandsurrender Jul 9, 2024
ea7d6f7
doc tweak
obliviateandsurrender Jul 10, 2024
b4a6e3c
Merge branch 'master' into noise-model-convert
obliviateandsurrender Jul 10, 2024
134b4f0
thermal error edge cases
obliviateandsurrender Jul 12, 2024
ab7a11e
happy `black`
obliviateandsurrender Jul 12, 2024
7d6333a
optional readout
obliviateandsurrender Jul 12, 2024
b6f1a1f
lil twix
obliviateandsurrender Jul 12, 2024
9016f22
doc tweak
obliviateandsurrender Jul 12, 2024
ab64443
add example
obliviateandsurrender Jul 12, 2024
48ada9a
black fix
obliviateandsurrender Jul 12, 2024
5b03736
tweak example
obliviateandsurrender Jul 12, 2024
1a8b4ee
update gate times
obliviateandsurrender Jul 13, 2024
061ca00
happy black
obliviateandsurrender Jul 13, 2024
d2cfc4a
improve gate times
obliviateandsurrender Jul 13, 2024
8d9e9ff
gate times test
obliviateandsurrender Jul 13, 2024
9f5f96c
minor tweak
obliviateandsurrender Jul 13, 2024
123cf43
minor tweaks
obliviateandsurrender Jul 14, 2024
787c85e
docs tweak
obliviateandsurrender Jul 14, 2024
c2af70d
tweaks
obliviateandsurrender Jul 14, 2024
ff73066
tweak
obliviateandsurrender Jul 15, 2024
4b97f4d
whitespaces
obliviateandsurrender Jul 15, 2024
cfb6304
doc fix?
obliviateandsurrender Jul 15, 2024
1af5805
improve a test
obliviateandsurrender Jul 15, 2024
53c26c1
changelog
obliviateandsurrender Jul 15, 2024
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
65 changes: 61 additions & 4 deletions pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
This module contains functions for converting Qiskit QuantumCircuit objects
into PennyLane circuit templates.
"""
from typing import Dict, Any, Iterable, Sequence, Union
import warnings
from functools import partial, reduce
from typing import Any, Dict, Iterable, Sequence, Union

import numpy as np
import pennylane as qml
import pennylane.ops as pennylane_ops
import qiskit.qasm2
from pennylane.noise.conditionals import WiresIn, _rename
from pennylane.operation import AnyWires
from qiskit import QuantumCircuit
from qiskit.circuit import Barrier, Clbit, ControlFlowOp, Measure
from qiskit.circuit import Parameter, ParameterExpression, ParameterVector
from qiskit.circuit import Measure, Barrier, ControlFlowOp, Clbit
from qiskit.circuit.classical import expr
from qiskit.circuit.controlflow.switch_case import _DefaultCaseType
from qiskit.circuit.library import GlobalPhaseGate
Expand All @@ -32,10 +36,10 @@
from qiskit.quantum_info import SparsePauliOp
from sympy import lambdify

import pennylane as qml
import pennylane.ops as pennylane_ops
from pennylane_qiskit.qiskit_device import QISKIT_OPERATION_MAP

from .noise_models import _build_noise_model_map

# pylint: disable=too-many-instance-attributes

inv_map = {v.__name__: k for k, v in QISKIT_OPERATION_MAP.items()}
Expand Down Expand Up @@ -1042,3 +1046,56 @@ def _expr_eval_clvals(clbits, clvals, expr_func, bitwise=False):
condition_res = expr_func(meas1, clreg2)

return condition_res


def load_noise_model(noise_model, **kwargs) -> qml.NoiseModel:
"""Loads a PennyLane ``NoiseModel`` from a Qiskit `NoiseModel
<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 conversion of the noise model.

Keyword Arguments:
gate_times (Dict[str, float]): gate times for building thermal relaxation error.
If not provided, the default value of ``1.0`` will be used for construction.
decimals (int): number of decimal places to round the Kraus matrices for errors to.
If not provided, the default value of ``10`` is used.
atol (float): the relative tolerance parameter. Default value is ``1e-05``.
rtol (float): the absolute tolernace parameters. Defualt value is ``1e-08``.
optimize (bool): controls if intermediate optimization is used while transforming Kraus
operators to a Choi matrix, wherever required. Default is ``False``.
kraus_shape (bool): use shape of the Kraus operators to display ``qml.QubitChannel``
instead of the complete list of matrices. Default is ``True``.

Returns:
qml.NoiseModel: An equivalent noise model constructed in PennyLane

Raises:
ValueError: When an encountered quantum error cannoted be converted.

.. note::

Currently, PennyLane noise models does not support readout errors, so those will be skipped during conversion.
"""

qerror_dmap, _ = _build_noise_model_map(noise_model, **kwargs)
model_map = {}
for error, wires_map in qerror_dmap.items():
conditions = []
cwires = []
for wires, operations in wires_map.items():
cond = qml.noise.op_in(operations)
if wires != AnyWires:
cond &= WiresIn(wires)
cwires.append(wires)
conditions.append(cond)
fcond = reduce(lambda cond1, cond2: cond1 | cond2, conditions)

noise = qml.noise.partial_wires(error)
if isinstance(error, qml.QubitChannel) and kwargs.get("kraus_shape", True):
noise = _rename(f"QubitChannel(Klist=Tensor{qml.math.shape(error.data)})")(noise)

model_map[fcond] = noise

return qml.NoiseModel(model_map)
Loading
Loading