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

Support loading Qiskit circuits with a single measurement #466

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
measurements by providing a list of PennyLane
`measurements <https://docs.pennylane.ai/en/stable/introduction/measurements.html>`_ themselves.
[(#405)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/405)
[(#466)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/466)

* Added the support for converting conditional operations based on mid-circuit measurements and
two of the ``ControlFlowOp`` operations - ``IfElseOp`` and ``SwitchCaseOp`` when converting
Expand Down
18 changes: 11 additions & 7 deletions pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
This module contains functions for converting Qiskit QuantumCircuit objects
into PennyLane circuit templates.
"""
from typing import Dict, Any, Sequence, Union
from typing import Dict, Any, Iterable, Sequence, Union
import warnings
from functools import partial, reduce

Expand Down Expand Up @@ -349,12 +349,12 @@ def load(quantum_circuit: QuantumCircuit, measurements=None):

Args:
quantum_circuit (qiskit.QuantumCircuit): the QuantumCircuit to be converted
measurements (list[pennylane.measurements.MeasurementProcess]): the list of PennyLane
`measurements <https://docs.pennylane.ai/en/stable/introduction/measurements.html>`_
that overrides the terminal measurements that may be present in the input circuit.
measurements (None | pennylane.measurements.MeasurementProcess | list[pennylane.measurements.MeasurementProcess]):
the PennyLane `measurements <https://docs.pennylane.ai/en/stable/introduction/measurements.html>`_
that override the terminal measurements that may be present in the input circuit

Returns:
function: the resulting PennyLane template
function: The resulting PennyLane template.
"""

# pylint:disable=too-many-branches, fixme, protected-access
Expand Down Expand Up @@ -553,9 +553,13 @@ def _function(*args, params: dict = None, wires: list = None, **kwargs):

# Use the user-provided measurements
if measurements:
if qml.queuing.QueuingManager.active_context():
if not qml.queuing.QueuingManager.active_context():
return measurements

if isinstance(measurements, Iterable):
return [qml.apply(meas) for meas in measurements]
return measurements

return qml.apply(measurements)

return tuple(mid_circ_meas + list(map(qml.measure, terminal_meas))) or None

Expand Down
24 changes: 22 additions & 2 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,28 @@ def cost(x, y):

assert np.allclose(jac, jac_expected)

def test_meas_circuit_in_qnode(self, qubit_device_2_wires):
"""Tests loading a converted template in a QNode with measurements."""
def test_quantum_circuit_with_single_measurement(self, qubit_device_single_wire):
"""Tests loading a converted template in a QNode with a single measurement."""
qc = QuantumCircuit(1)
qc.h(0)
qc.measure_all()

measurement = qml.expval(qml.PauliZ(0))
quantum_circuit = load(qc, measurements=measurement)

@qml.qnode(qubit_device_single_wire)
def circuit_loaded_qiskit_circuit():
return quantum_circuit()

@qml.qnode(qubit_device_single_wire)
def circuit_native_pennylane():
qml.Hadamard(0)
return qml.expval(qml.PauliZ(0))

assert circuit_loaded_qiskit_circuit() == circuit_native_pennylane()

def test_quantum_circuit_with_multiple_measurements(self, qubit_device_2_wires):
"""Tests loading a converted template in a QNode with multiple measurements."""

angle = 0.543

Expand Down
Loading