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

Fix conversion of controllled gates #136

Merged
merged 19 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Unreleased
----------

* Fix conversion of qiskit `UnitaryGate` to and from pytket (up to 3 qubits).
* Fix handling of qiskit controlled gates in the :py:meth:`qiskit_to_tk` converter.
* Handle CCZ and CSX gates in circuit converters.

0.40.0 (June 2023)
------------------
Expand Down
14 changes: 10 additions & 4 deletions pytket/extensions/qiskit/qiskit_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
qiskit_gates.CU1Gate: OpType.CU1,
qiskit_gates.CU3Gate: OpType.CU3,
qiskit_gates.CXGate: OpType.CX,
qiskit_gates.CSXGate: OpType.CSX,
qiskit_gates.CYGate: OpType.CY,
qiskit_gates.CZGate: OpType.CZ,
qiskit_gates.ECRGate: OpType.ECR,
Expand All @@ -139,6 +140,7 @@
qiskit_gates.C3XGate: OpType.CnX,
qiskit_gates.C4XGate: OpType.CnX,
qiskit_gates.CCXGate: OpType.CCX,
qiskit_gates.CCZGate: OpType.CnZ,
qiskit_gates.CSwapGate: OpType.CSWAP,
# Multi-controlled gates (qiskit expects a list of controls followed by the target):
qiskit_gates.MCXGate: OpType.CnX,
Expand Down Expand Up @@ -350,8 +352,12 @@ def add_qiskit_data(self, data: "QuantumCircuitData") -> None:
pass
self.add_xs(num_ctrl_qubits, ctrl_state, qargs)
optype = None
if type(instr) == ControlledGate:
if type(instr.base_gate) == qiskit_gates.RYGate:
if isinstance(instr, ControlledGate):
if type(instr) in _known_qiskit_gate:
# First we check that the gate isn't in _known_qiskit_gate
# this avoids CZ being converted to CnZ
optype = _known_qiskit_gate[type(instr)]
CalMacCQ marked this conversation as resolved.
Show resolved Hide resolved
elif type(instr.base_gate) == qiskit_gates.RYGate:
optype = OpType.CnRy
elif type(instr.base_gate) == qiskit_gates.YGate:
optype = OpType.CnY
Expand All @@ -377,8 +383,8 @@ def add_qiskit_data(self, data: "QuantumCircuitData") -> None:
params = [param_to_tk(p) for p in instr.base_gate.params]
n_base_qubits = instr.base_gate.num_qubits
sub_circ = Circuit(n_base_qubits)
# use base gate name for the CircBox (shows in renderer)
sub_circ.name = instr.base_gate.name.capitalize()
# Use U for the name of the base gate in QControlBox
cqc-melf marked this conversation as resolved.
Show resolved Hide resolved
sub_circ.name = "U"
sub_circ.add_gate(base_tket_gate, params, list(range(n_base_qubits)))
c_box = CircBox(sub_circ)
q_ctrl_box = QControlBox(c_box, instr.num_ctrl_qubits)
Expand Down
39 changes: 39 additions & 0 deletions tests/qiskit_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,42 @@ def test_unitary_gate() -> None:
assert cmds[0].op.type == OpType.Unitary1qBox
assert cmds[1].op.type == OpType.Unitary2qBox
assert cmds[2].op.type == OpType.Unitary3qBox


def test_ccz_conversion() -> None:
qc_ccz = QuantumCircuit(4)
qc_ccz.append(qiskit_gates.CCZGate(), [0, 1, 2])
qc_ccz.append(qiskit_gates.CCZGate(), [3, 1, 0])
tkc_ccz = qiskit_to_tk(qc_ccz)
assert tkc_ccz.n_gates_of_type(OpType.CnZ) == tkc_ccz.n_gates == 2
cqc-melf marked this conversation as resolved.
Show resolved Hide resolved
# bidirectional CnZ conversion already supported


def test_csx_conversion() -> None:
qc_csx = QuantumCircuit(2)
qc_csx.append(qiskit_gates.CSXGate(), [0, 1])
qc_csx.append(qiskit_gates.CSXGate(), [1, 0])
converted_tkc = qiskit_to_tk(qc_csx)
assert converted_tkc.n_gates == 2
assert converted_tkc.n_gates_of_type(OpType.CSX) == 2
u1 = converted_tkc.get_unitary()
new_tkc_csx = Circuit(2)
new_tkc_csx.add_gate(OpType.CSX, [0, 1]).add_gate(OpType.CSX, [1, 0])
u2 = new_tkc_csx.get_unitary()
assert compare_unitaries(u1, u2)
converted_qc = tk_to_qiskit(new_tkc_csx)
assert converted_qc.count_ops()["csx"] == 2
qc_c3sx = QuantumCircuit(4)
qc_c3sx.append(qiskit_gates.C3SXGate(), [0, 1, 2, 3])
tkc_c3sx = qiskit_to_tk(qc_c3sx)
cqc-melf marked this conversation as resolved.
Show resolved Hide resolved
assert tkc_c3sx.n_gates == tkc_c3sx.n_gates_of_type(OpType.QControlBox) == 1


def test_CS_and_CSdg() -> None:
qiskit_qc = QuantumCircuit(2)
qiskit_qc.append(qiskit_gates.CSGate(), [0, 1])
qiskit_qc.append(qiskit_gates.CSdgGate(), [0, 1])
qiskit_qc.append(qiskit_gates.CSGate(), [1, 0])
qiskit_qc.append(qiskit_gates.CSdgGate(), [1, 0])
tkc = qiskit_to_tk(qiskit_qc)
assert tkc.n_gates_of_type(OpType.QControlBox) == 4