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

Diagonalize gates #558

Merged
merged 152 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
20be42a
removed legacy ci
austingmhuang May 16, 2024
a2dade8
Delete .github/workflows/tests.yml
austingmhuang May 16, 2024
2c9ca56
Deleted use_primitives from tests
austingmhuang May 16, 2024
f25d567
remove use_primitive kwarg and things that depend on it
austingmhuang May 16, 2024
6e743f8
fix tests and split_exec
austingmhuang May 16, 2024
ca52cd0
fixed test
austingmhuang May 16, 2024
76e011e
pylint
austingmhuang May 16, 2024
cc9eb07
Merge branch 'update_reqs' into remove_use_primitives
austingmhuang May 16, 2024
de61385
change to v2 prims, del Options
austingmhuang May 16, 2024
769369e
del Options
austingmhuang May 16, 2024
0bc004b
temp changes to options
austingmhuang May 17, 2024
61b866f
naming and 0.46 test
austingmhuang May 17, 2024
78a7131
rename
austingmhuang May 17, 2024
fd87f82
update qiskit device
austingmhuang May 17, 2024
1b0d3af
dep warnings
austingmhuang May 17, 2024
7f13512
pylint
austingmhuang May 17, 2024
00337ee
Merge branch 'update_reqs' into remove_use_primitives
austingmhuang May 17, 2024
17325a2
changes to tests
austingmhuang May 21, 2024
05b346f
Merge branch 'update_reqs' into remove_use_primitives
austingmhuang May 21, 2024
e29ff38
Merge branch 'remove_use_primitives' into migrate_to_v2_primitives
austingmhuang May 21, 2024
fbf7279
deleted options for now
austingmhuang May 21, 2024
dbd0806
small changes
austingmhuang May 22, 2024
b09755d
Merge branch 'new_device_feature_branch' into remove_use_primitives
austingmhuang May 23, 2024
7dd0f21
Merge branch 'remove_use_primitives' into migrate_to_v2_primitives
austingmhuang May 23, 2024
ba3e944
access sampler results
austingmhuang May 23, 2024
2b13409
Sampler tests and functionality
austingmhuang May 23, 2024
8693f5f
estimator multi measurement works
austingmhuang May 23, 2024
46b6fe0
estimator now gives variances
austingmhuang May 23, 2024
29c4eb3
comments
austingmhuang May 23, 2024
261d7c2
removed backend.run() and _execute_runtime
austingmhuang May 23, 2024
7a40f1c
remove additional stuff
austingmhuang May 24, 2024
e41cd3d
linter
austingmhuang May 24, 2024
710bcb7
merge confs
austingmhuang May 24, 2024
b7447e2
docstring changes
austingmhuang May 24, 2024
73019a2
Merge branch 'remove_use_primitives' into migrate_to_v2_primitives
austingmhuang May 24, 2024
cfffed2
skip additional test
austingmhuang May 24, 2024
d71f440
Merge branch 'remove_use_primitives' into migrate_to_v2_primitives
austingmhuang May 24, 2024
28b4f98
[skip ci] format is correct, checks probs as well
austingmhuang May 24, 2024
c07d550
[skip ci] docstring
austingmhuang May 24, 2024
d6dd44b
docstrings
austingmhuang May 24, 2024
90d792a
Merge branch 'migrate_to_v2_primitives' into process_kwargs
austingmhuang May 24, 2024
02f9ee7
We delete the Options Handling class because there are no more Option…
austingmhuang May 24, 2024
e8f9002
changed warnings due to difference in UI for setting shots between Qi…
austingmhuang May 24, 2024
e7d2630
un did confusing change that didnt do anything
austingmhuang May 27, 2024
6441344
rerun ci
austingmhuang May 27, 2024
eaa9727
Syntax changes due to version change
austingmhuang May 27, 2024
cd3e7b2
Delete for codecov
austingmhuang May 27, 2024
b704207
Update tests/test_base_device.py
austingmhuang May 27, 2024
1f84aec
Update pennylane_qiskit/qiskit_device2.py
austingmhuang May 27, 2024
1d5370a
Update tests/test_base_device.py
austingmhuang May 27, 2024
1f2d4f9
docstrings
austingmhuang May 24, 2024
c203e48
Update tests/test_base_device.py
austingmhuang May 27, 2024
3652385
Update pennylane_qiskit/qiskit_device2.py
austingmhuang May 27, 2024
8d58148
Update tests/test_base_device.py
austingmhuang May 27, 2024
1f461d9
[skip ci] merge confs
austingmhuang May 27, 2024
026c0c7
Update tests/test_base_device.py
austingmhuang May 28, 2024
d924fa1
Update pennylane_qiskit/qiskit_device2.py
austingmhuang May 28, 2024
b15e31d
Merge branch 'remove_use_primitives' into migrate_to_v2_primitives
austingmhuang May 28, 2024
ac8833d
finishing touches
austingmhuang May 28, 2024
cd60bf9
Merge branch 'migrate_to_v2_primitives' into process_kwargs
austingmhuang May 28, 2024
8a61f34
comments
austingmhuang May 28, 2024
5bcb33a
[skip ci] formatting
austingmhuang May 28, 2024
fcb8767
[skip ci] refactor
austingmhuang May 28, 2024
089a7c9
Merge branch 'migrate_to_v2_primitives' into process_kwargs
austingmhuang May 28, 2024
729eeb2
Merge branch 'new_device_feature_branch' into migrate_to_v2_primitives
austingmhuang May 28, 2024
bd3a9b6
due to the fact that shots are not tracked in the estimator's metadat…
austingmhuang May 28, 2024
54bc21d
black
austingmhuang May 28, 2024
4fcb5e3
lint
austingmhuang May 28, 2024
f9f4f18
docstring changes
austingmhuang May 28, 2024
335c26e
backend options?
austingmhuang May 28, 2024
e8005c6
deleting unused tests
austingmhuang May 28, 2024
cc44aee
line change
austingmhuang May 28, 2024
c2ed32b
[skip ci] changed test to be more readable
austingmhuang May 29, 2024
dc90fa6
New tests for options functionality and edge case
austingmhuang May 29, 2024
cf66bf1
pylint
austingmhuang May 30, 2024
bcc1652
Merge branch 'migrate_to_v2_primitives' into process_kwargs
austingmhuang May 30, 2024
f3f1a73
We make sure that transpilation options are not passed to the primiti…
austingmhuang May 30, 2024
0e1f7fb
Due to changing the signature of get_transpile_args(), we need to fix…
austingmhuang May 30, 2024
8dadf7f
warning message for default_shots was unclear. changed to be more clear
austingmhuang May 30, 2024
78e102f
add more comments
austingmhuang May 30, 2024
d9c8dac
yay
austingmhuang May 30, 2024
d4163bb
docstring
austingmhuang May 30, 2024
7519b80
Merge branch 'migrate_to_v2_primitives' into process_kwargs
austingmhuang May 30, 2024
e58ce1c
Testing to ensure that options and kwargs combine properly
austingmhuang May 30, 2024
809e4ef
edit tests for pylint
austingmhuang May 30, 2024
b8153df
Update pennylane_qiskit/qiskit_device2.py
austingmhuang May 31, 2024
bd4c386
Update pennylane_qiskit/qiskit_device2.py
austingmhuang May 31, 2024
43fb89e
edit test regex matching due to changes earlier
austingmhuang May 31, 2024
34d1ada
refactoring of estimator and sampler
austingmhuang May 31, 2024
bb054b8
generate samples tested
austingmhuang May 31, 2024
790802b
process_estimator_job tests
austingmhuang May 31, 2024
a056911
comment for clarity
austingmhuang May 31, 2024
aa65304
merge conflicts
austingmhuang May 31, 2024
3429b40
pylint
austingmhuang May 31, 2024
1520414
pylint
austingmhuang May 31, 2024
6e9969d
Merge branch 'new_device_feature_branch' into migrate_to_v2_primitives
austingmhuang May 31, 2024
c02adbb
Merge branch 'migrate_to_v2_primitives' into process_kwargs
austingmhuang May 31, 2024
f733a27
[skip ci] minor formatting
austingmhuang Jun 4, 2024
c863740
Fix unintended additonal dimensionality and added test for res != 1 t…
austingmhuang Jun 4, 2024
a16dcfe
fix to transpiles
austingmhuang Jun 4, 2024
b108fef
comments to explain some stuff
austingmhuang Jun 4, 2024
5e33456
Merge branch 'new_device_feature_branch' into process_kwargs
austingmhuang Jun 4, 2024
b08dd7e
merge conflicts
austingmhuang Jun 4, 2024
e34f2ab
docstring
austingmhuang Jun 6, 2024
5e4d8dd
Update pennylane_qiskit/qiskit_device2.py
austingmhuang Jun 7, 2024
fb03ce7
Update pennylane_qiskit/qiskit_device2.py
austingmhuang Jun 7, 2024
da2fe8e
Update pennylane_qiskit/qiskit_device2.py
austingmhuang Jun 7, 2024
1ef119e
Update pennylane_qiskit/qiskit_device2.py
austingmhuang Jun 7, 2024
dbaddca
Update pennylane_qiskit/qiskit_device2.py
austingmhuang Jun 7, 2024
d66f441
clean up
austingmhuang Jun 7, 2024
6193aca
refactor
austingmhuang Jun 7, 2024
b742e3d
pylint
austingmhuang Jun 7, 2024
81d9d8f
formatting of docstring
austingmhuang Jun 7, 2024
d0948d4
revert to tuple(res)
austingmhuang Jun 10, 2024
770580f
[skip ci] fix to dimensions of sampler
austingmhuang Jun 10, 2024
c537c3e
docstrings
austingmhuang Jun 11, 2024
a8846d4
some docstrings changes
austingmhuang Jun 11, 2024
eb8319f
Update tests/test_base_device.py
austingmhuang Jun 11, 2024
c336c5b
black
austingmhuang Jun 11, 2024
d88d8ec
[skip ci] import split_non_commuting
austingmhuang Jun 11, 2024
ce754d8
diagonalize tests for Hadamard
austingmhuang Jun 11, 2024
cf162ae
changed stopping condition to reflect reality of what's supported and…
austingmhuang Jun 11, 2024
d642a3b
[skip ci] added comment about qml.var not providing matching answers
austingmhuang Jun 11, 2024
fc6bd1b
some tests
austingmhuang Jun 12, 2024
b1b467a
Merge branch 'new_device_feature_branch' into process_kwargs
austingmhuang Jun 12, 2024
282b11f
Merge branch 'process_kwargs' into diagonalize_gates
austingmhuang Jun 12, 2024
bde84f7
[skip ci] linter
austingmhuang Jun 12, 2024
cceeb66
interesting changes
austingmhuang Jun 12, 2024
d0ff6db
linter
austingmhuang Jun 12, 2024
2c82c41
Merge branch 'new_device_feature_branch' into process_kwargs
austingmhuang Jun 13, 2024
e8d579f
Merge branch 'new_device_feature_branch' into process_kwargs
austingmhuang Jun 13, 2024
7430d54
Merge branch 'process_kwargs' into diagonalize_gates
austingmhuang Jun 13, 2024
03a1f03
split non commuting test cases
austingmhuang Jun 13, 2024
587bb31
Merge branch 'new_device_feature_branch' into diagonalize_gates
austingmhuang Jun 13, 2024
8422f45
sprod
austingmhuang Jun 13, 2024
9fdc0aa
sampler tested as well
austingmhuang Jun 13, 2024
008fc62
linter
austingmhuang Jun 13, 2024
4344a42
black
austingmhuang Jun 13, 2024
3390647
docstrings and comments
austingmhuang Jun 13, 2024
d92d191
docstrings and comments black
austingmhuang Jun 13, 2024
558bbbb
comment regarding magic number
austingmhuang Jun 13, 2024
e4b064f
todo
austingmhuang Jun 14, 2024
3bf1f4e
add np
austingmhuang Jun 14, 2024
ab1467e
more concise way of testing
austingmhuang Jun 14, 2024
3661e07
Update tests/test_base_device.py
austingmhuang Jun 20, 2024
963b3da
Update tests/test_base_device.py
austingmhuang Jun 20, 2024
8281c7c
fix black
austingmhuang Jun 20, 2024
1a3a27b
diagonalize for edge case
austingmhuang Jun 20, 2024
46b477b
linter
austingmhuang Jun 20, 2024
fd8ceec
pylint
austingmhuang Jun 20, 2024
c04665a
clean up
austingmhuang Jun 21, 2024
924b093
fix docstring
austingmhuang Jun 21, 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
7 changes: 6 additions & 1 deletion pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

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

inv_map = {v.__name__: k for k, v in QISKIT_OPERATION_MAP.items()}
Expand Down Expand Up @@ -652,7 +653,11 @@ def circuit_to_qiskit(circuit, register_size, diagonalize=True, measure=True):
# rotate the state for measurement in the computational basis
# ToDo: check this in cases with multiple different bases
if diagonalize:
rotations = circuit.diagonalizing_gates
rotations, measurements = rotations_and_diagonal_measurements(circuit)
for _, m in enumerate(measurements):
if m.obs is not None:
rotations.extend(m.obs.diagonalizing_gates())

for rot in rotations:
qc &= operation_to_qiskit(rot, reg, creg)

Expand Down
12 changes: 7 additions & 5 deletions pennylane_qiskit/qiskit_device2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from pennylane import transform
from pennylane.transforms.core import TransformProgram
from pennylane.transforms import broadcast_expand
from pennylane.transforms import broadcast_expand, split_non_commuting
from pennylane.tape import QuantumTape, QuantumScript
from pennylane.typing import Result, ResultBatch
from pennylane.devices import Device
Expand Down Expand Up @@ -129,7 +129,6 @@ def split_execution_types(
will use the Qiskit Sampler. ExpectationValue and Variance will use the Estimator, except
when the measured observable does not have a `pauli_rep`. In that case, the Sampler will be
used, and the raw samples will be processed to give an expectation value."""

estimator = []
sampler = []

Expand Down Expand Up @@ -219,6 +218,11 @@ class QiskitDevice2(Device):
"Hadamard",
"Hermitian",
"Projector",
"Prod",
"Sum",
"LinearCombination",
"SProd",
austingmhuang marked this conversation as resolved.
Show resolved Hide resolved
# TODO Could support SparseHamiltonian
}

# pylint:disable = too-many-arguments
Expand Down Expand Up @@ -365,7 +369,7 @@ def preprocess(
)

transform_program.add_transform(broadcast_expand)
# missing: split non-commuting, sum_expand, etc. [SC-62047]
transform_program.add_transform(split_non_commuting)

transform_program.add_transform(split_execution_types)

Expand Down Expand Up @@ -580,10 +584,8 @@ def _process_estimator_job(measurements, job_result):
Returns:
result (tuple): the processed result from EstimatorV2
"""

expvals = job_result[0].data.evs
variances = (job_result[0].data.stds / job_result[0].metadata["target_precision"]) ** 2

result = []
for i, mp in enumerate(measurements):
if isinstance(mp, ExpectationMP):
Expand Down
227 changes: 225 additions & 2 deletions tests/test_base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,75 @@ def test_stopping_conditions(self, op, expected):
[
(qml.PauliX(0), True),
(qml.Hadamard(3), True),
(qml.prod(qml.PauliY(1), qml.PauliZ(0)), False),
(qml.prod(qml.PauliY(1), qml.PauliZ(0)), True),
],
)
def test_observable_stopping_condition(self, obs, expected):
"""Test that observable_stopping_condition works"""
res = test_dev.observable_stopping_condition(obs)
assert res == expected

@pytest.mark.parametrize(
"measurements, num_tapes",
[
(
[
qml.expval(qml.X(0) + qml.Y(0) + qml.Z(0)),
],
3,
),
(
[qml.var(qml.X(0) + qml.Y(0) + qml.Z(0))], # Var should not split
1,
),
(
[
qml.expval(qml.X(0)),
qml.expval(qml.Y(1)),
qml.expval(qml.Z(0) @ qml.Z(1)),
qml.expval(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
],
3,
),
(
[
qml.expval(
qml.prod(qml.X(0), qml.Z(0), qml.Z(0)) + 0.35 * qml.X(0) - 0.21 * qml.Z(0)
)
],
2,
),
(
[
qml.counts(qml.X(0)),
qml.counts(qml.Y(1)),
qml.counts(qml.Z(0) @ qml.Z(1)),
qml.counts(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
],
3,
),
(
[
qml.sample(qml.X(0)),
qml.sample(qml.Y(1)),
qml.sample(qml.Z(0) @ qml.Z(1)),
qml.sample(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
],
3,
),
],
)
def test_preprocess_split_non_commuting(self, measurements, num_tapes):
"""Test that `split_non_commuting` works as expected in the preprocess function."""

dev = QiskitDevice2(wires=5, backend=backend)
qs = QuantumScript([], measurements=measurements, shots=qml.measurements.Shots(1000))

program, _ = dev.preprocess()
tapes, _ = program([qs])

assert len(tapes) == num_tapes

@pytest.mark.parametrize(
"measurements,num_types",
[
Expand Down Expand Up @@ -451,7 +512,6 @@ def test_warning_if_shots(self):
match="default_shots was found in the keyword arguments",
):
dev = QiskitDevice2(wires=2, backend=backend, options={"default_shots": 30})

# resets to default since we reinitialize the device
assert dev._kwargs["default_shots"] == 1024

Expand Down Expand Up @@ -1114,3 +1174,166 @@ def qiskit_circuit(x):
assert np.shape(res[0]) == np.shape(qiskit_res[0])
assert np.shape(res[1]) == np.shape(qiskit_res[1])
assert len(res) == len(qiskit_res)

@pytest.mark.parametrize(
"observable",
[
lambda: [qml.expval(qml.Hadamard(0)), qml.expval(qml.Hadamard(0))],
lambda: [
qml.var(qml.Hadamard(0)),
qml.var(qml.Hadamard(0)),
],
lambda: [
qml.expval(qml.X(0)),
qml.expval(qml.Y(1)),
qml.expval(0.5 * qml.Y(1)),
qml.expval(qml.Z(0) @ qml.Z(1)),
qml.expval(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
qml.expval(
qml.ops.LinearCombination(
[0.35, 0.46], [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.X(2)]
)
),
qml.expval(
qml.ops.LinearCombination(
[1.0, 2.0, 3.0], [qml.X(0), qml.X(1), qml.Z(0)], grouping_type="qwc"
)
),
],
lambda: [
qml.expval(
qml.Hamiltonian([0.35, 0.46], [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.Y(2)])
)
],
lambda: [qml.expval(qml.X(0) @ qml.Z(1) + qml.Z(0))],
pytest.param(
[qml.var(qml.X(0) + qml.Z(0))],
marks=pytest.mark.xfail(reason="Qiskit itself is bugged when given Sum"),
),
lambda: [
qml.expval(qml.Hadamard(0)),
qml.expval(qml.Hadamard(1)),
qml.expval(qml.Hadamard(0) @ qml.Hadamard(1)),
qml.expval(
qml.Hadamard(0) @ qml.Hadamard(1) + 0.5 * qml.Hadamard(1) + qml.Hadamard(0)
),
],
],
)
def test_observables_that_need_split_non_commuting(self, observable):
"""Tests that observables that have non-commuting measurements are
processed correctly when executed by the Estimator or, in the case of
qml.Hadamard, executed by the Sampler via expval() or var"""
qiskit_dev = QiskitDevice2(wires=3, backend=backend, shots=30000)

@qml.qnode(qiskit_dev)
def qiskit_circuit():
qml.RX(np.pi / 3, 0)
qml.RZ(np.pi / 3, 0)
return observable()

dev = qml.device("default.qubit", wires=3, shots=30000)

@qml.qnode(dev)
def circuit():
qml.RX(np.pi / 3, 0)
qml.RZ(np.pi / 3, 0)
return observable()

qiskit_res = qiskit_circuit()
res = circuit()

assert np.allclose(res, qiskit_res, atol=0.05)

@pytest.mark.parametrize(
"observable",
[
lambda: [qml.counts(qml.X(0) + qml.Y(0)), qml.counts(qml.X(0))],
austingmhuang marked this conversation as resolved.
Show resolved Hide resolved
lambda: [
qml.counts(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
qml.counts(0.5 * qml.Y(1)),
],
],
)
def test_observables_that_need_split_non_commuting_counts(self, observable):
"""Tests that observables that have non-commuting measurents are processed
correctly when executed by the Sampler via counts()"""
qiskit_dev = QiskitDevice2(wires=3, backend=backend, shots=30000)

@qml.qnode(qiskit_dev)
def qiskit_circuit():
qml.RX(np.pi / 3, 0)
qml.RZ(np.pi / 3, 0)
return observable()

dev = qml.device("default.qubit", wires=3, shots=30000)

@qml.qnode(dev)
def circuit():
qml.RX(np.pi / 3, 0)
qml.RZ(np.pi / 3, 0)
return observable()

qiskit_res = qiskit_circuit()
res = circuit()

assert len(qiskit_res) == len(res)
for res1, res2 in zip(qiskit_res, res):
assert all(res1[key] - res2.get(key, 0) < 300 for key in res1)

@pytest.mark.parametrize(
"observable",
[
lambda: [qml.sample(qml.X(0) + qml.Y(0)), qml.sample(qml.X(0))],
lambda: [qml.sample(qml.X(0) @ qml.Y(1)), qml.sample(qml.X(0))],
lambda: [
qml.sample(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
qml.sample(0.5 * qml.Y(1)),
],
lambda: [
qml.sample(qml.X(0)),
qml.sample(qml.Y(1)),
qml.sample(0.5 * qml.Y(1)),
qml.sample(qml.Z(0) @ qml.Z(1)),
qml.sample(qml.X(0) @ qml.Z(1) + 0.5 * qml.Y(1) + qml.Z(0)),
qml.sample(
qml.ops.LinearCombination(
[0.35, 0.46], [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.X(2)]
)
),
qml.sample(
qml.ops.LinearCombination(
[1.0, 2.0, 3.0], [qml.X(0), qml.X(1), qml.Z(0)], grouping_type="qwc"
)
),
],
lambda: [
qml.sample(
qml.Hamiltonian([0.35, 0.46], [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.Y(2)])
)
],
],
)
def test_observables_that_need_split_non_commuting_samples(self, observable):
"""Tests that observables that have non-commuting measurents are processed
correctly when executed by the Sampler via sample()"""
qiskit_dev = QiskitDevice2(wires=3, backend=backend, shots=30000)

@qml.qnode(qiskit_dev)
def qiskit_circuit():
qml.RX(np.pi / 3, 0)
qml.RZ(np.pi / 3, 0)
return observable()

dev = qml.device("default.qubit", wires=3, shots=30000)

@qml.qnode(dev)
def circuit():
qml.RX(np.pi / 3, 0)
qml.RZ(np.pi / 3, 0)
return observable()

qiskit_res = qiskit_circuit()
res = circuit()

assert np.allclose(np.mean(qiskit_res, axis=1), np.mean(res, axis=1), atol=0.05)
23 changes: 23 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,29 @@ def test_circuit_to_qiskit_diagonalize_kwarg(self, diagonalize):

assert len(instructions) == len(expected_gates)

def test_circuit_to_qiskit_measurements_with_overlapping_wires(self):
"""Test that diagonalizing gates work for circuits with
measurements on overlapping wires"""

measurements = [qml.sample(qml.X(0) @ qml.Y(1)), qml.sample(qml.X(0))]
tape = qml.tape.QuantumScript(measurements=measurements)

qc = circuit_to_qiskit(tape, 2, diagonalize=True, measure=True)

# get list of instruction names up to the barrier (played right before measurements)
instructions = []
for instruction in qc.data:
if instruction.operation.name == "barrier":
break
instructions.append(instruction.operation.name)

# manually diagonalized test case since Qiskit transpiles whatever we had before
# and that results is different from PL's diagonalization
expected_gates = ["ry", "rx"]

assert len(instructions) == len(expected_gates)
assert instructions == expected_gates


class TestConverterGatePennyLaneToQiskit:
def test_non_parameteric_operation_to_qiskit(self):
Expand Down
Loading