Skip to content

Commit 59d733d

Browse files
authored
fix: Allow circuits containing CnZ gates to run on AerBackend (#369)
1 parent 30855b1 commit 59d733d

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

docs/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Unreleased
1313
* Add conversion of controlled unitary gates from qiskit to tket.
1414
* Initialize `TketAutoPass` with a `BackendV2`.
1515
* Update `TketBackend` to derive from `BackendV2`.
16+
* Fix to allow `AerBackend` to work with multi-controlled Z gates.
1617

1718
0.55.0 (July 2024)
1819
------------------

pytket/extensions/qiskit/qiskit_convert.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,9 @@ def append_tk_command_to_qiskit(
758758
if optype == OpType.CnY:
759759
return qcirc.append(qiskit_gates.YGate().control(len(qargs) - 1), qargs)
760760
if optype == OpType.CnZ:
761-
return qcirc.append(qiskit_gates.ZGate().control(len(qargs) - 1), qargs)
761+
new_gate = qiskit_gates.ZGate().control(len(qargs) - 1)
762+
new_gate.name = "mcz"
763+
return qcirc.append(new_gate, qargs)
762764
if optype == OpType.CnRy:
763765
# might as well do a bit more checking
764766
assert len(op.params) == 1

tests/backend_test.py

+14
Original file line numberDiff line numberDiff line change
@@ -1498,3 +1498,17 @@ def test_noisy_density_matrix_simulation() -> None:
14981498
assert noisy_dm.shape == (8, 8)
14991499
# Check purity to verify mixed state
15001500
assert np.trace(noisy_dm**2).real < 0.99
1501+
1502+
1503+
def test_mc_gate_on_aer() -> None:
1504+
"""Test for cm gates support in aer simulators
1505+
https://github.com/CQCL/pytket-qiskit/issues/368"""
1506+
b = AerBackend()
1507+
c = Circuit(3, 3)
1508+
c.X(0).X(1)
1509+
c.H(2)
1510+
c.add_gate(OpType.CnZ, [0, 1, 2])
1511+
c.H(2)
1512+
c.measure_all()
1513+
r = b.run_circuit(c, n_shots=10)
1514+
assert r.get_counts() == Counter({(1, 1, 1): 10})

tests/qiskit_convert_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ def test_multicontrolled_gate_conversion() -> None:
828828
assert my_tkc.n_gates_of_type(OpType.CnRy) == 2
829829
my_new_qc = tk_to_qiskit(my_tkc)
830830
qiskit_ops = my_new_qc.count_ops()
831-
assert qiskit_ops["c3y"] and qiskit_ops["c3z"] and qiskit_ops["c3ry"] == 2
831+
assert qiskit_ops["c3y"] and qiskit_ops["mcz"] and qiskit_ops["c3ry"] == 2
832832
tcirc = qiskit_to_tk(my_new_qc)
833833
unitary_after = tcirc.get_unitary()
834834
assert compare_unitaries(unitary_before, unitary_after)
@@ -1034,7 +1034,7 @@ def test_ccz_conversion() -> None:
10341034
assert tkc_ccz.n_gates_of_type(OpType.CnZ) == tkc_ccz.n_gates == 2
10351035
# bidirectional CnZ conversion already supported
10361036
qc_ccz2 = tk_to_qiskit(tkc_ccz)
1037-
assert qc_ccz2.count_ops()["ccz"] == 2
1037+
assert qc_ccz2.count_ops()["mcz"] == 2
10381038
tkc_ccz2 = qiskit_to_tk(qc_ccz2)
10391039
assert compare_unitaries(tkc_ccz.get_unitary(), tkc_ccz2.get_unitary())
10401040

0 commit comments

Comments
 (0)