From 96d01b1f0a54715651399a663df33651a6a060b4 Mon Sep 17 00:00:00 2001 From: Callum Macpherson <93673602+CalMacCQ@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:05:01 +0100 Subject: [PATCH] refactor: minor improvement to `qiskit_to_tk` conversion. (#404) * use OpType.StatePreparationBox * refactor circbox conversion --------- Co-authored-by: cqc-melf <70640934+cqc-melf@users.noreply.github.com> --- pytket/extensions/qiskit/qiskit_convert.py | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/pytket/extensions/qiskit/qiskit_convert.py b/pytket/extensions/qiskit/qiskit_convert.py index cadea2ae..a5654dc9 100644 --- a/pytket/extensions/qiskit/qiskit_convert.py +++ b/pytket/extensions/qiskit/qiskit_convert.py @@ -464,6 +464,16 @@ def _get_pytket_condition_kwargs( raise NotImplementedError("condition must contain classical bit or register") +def _build_circbox(instr: Instruction, circuit: QuantumCircuit) -> CircBox: + qregs = [QuantumRegister(instr.num_qubits, "q")] if instr.num_qubits > 0 else [] + cregs = [ClassicalRegister(instr.num_clbits, "c")] if instr.num_clbits > 0 else [] + builder = CircuitBuilder(qregs, cregs) + builder.add_qiskit_data(circuit, instr.definition) + subc = builder.circuit() + subc.name = instr.name + return CircBox(subc) + + class CircuitBuilder: def __init__( self, @@ -524,7 +534,7 @@ def add_qiskit_data( q_ctrl_box = _get_qcontrol_box(c_gate=instr, params=params) self.tkc.add_qcontrolbox(q_ctrl_box, qubits) - elif isinstance(instr, (Initialize, StatePreparation)): + elif optype == OpType.StatePreparationBox: # Append OpType found by stateprep helpers _add_state_preparation(self.tkc, qubits, instr) @@ -552,22 +562,10 @@ def add_qiskit_data( elif optype == OpType.Barrier: self.tkc.add_barrier(qubits) + elif optype == OpType.CircBox: - qregs = ( - [QuantumRegister(instr.num_qubits, "q")] - if instr.num_qubits > 0 - else [] - ) - cregs = ( - [ClassicalRegister(instr.num_clbits, "c")] - if instr.num_clbits > 0 - else [] - ) - builder = CircuitBuilder(qregs, cregs) - builder.add_qiskit_data(circuit, instr.definition) - subc = builder.circuit() - subc.name = instr.name - self.tkc.add_circbox(CircBox(subc), qubits + bits, **condition_kwargs) # type: ignore + circbox = _build_circbox(instr, circuit) + self.tkc.add_circbox(circbox, qubits + bits, **condition_kwargs) # type: ignore elif optype == OpType.CU3 and type(instr) is qiskit_gates.CUGate: if instr.params[-1] == 0: