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 8 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
14 changes: 12 additions & 2 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,13 +352,21 @@ 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:
optype = _known_qiskit_gate[type(instr)]
CalMacCQ marked this conversation as resolved.
Show resolved Hide resolved
pass
elif type(instr.base_gate) == qiskit_gates.RYGate:
optype = OpType.CnRy
elif type(instr.base_gate) == qiskit_gates.YGate:
optype = OpType.CnY
elif type(instr.base_gate) == qiskit_gates.ZGate:
optype = OpType.CnZ
elif (
type(instr.base_gate) in _known_qiskit_gate
and type(instr) in _known_qiskit_gate
):
optype = _known_qiskit_gate[type(instr)]
else:
if type(instr.base_gate) in _known_qiskit_gate:
optype = OpType.QControlBox # QControlBox case handled below
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