Skip to content

Commit

Permalink
refactor: minor improvement to qiskit_to_tk conversion. (#404)
Browse files Browse the repository at this point in the history
* use OpType.StatePreparationBox

* refactor circbox conversion

---------

Co-authored-by: cqc-melf <[email protected]>
  • Loading branch information
CalMacCQ and cqc-melf authored Oct 24, 2024
1 parent 814ca98 commit 96d01b1
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions pytket/extensions/qiskit/qiskit_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 96d01b1

Please sign in to comment.