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

refactor: minor improvement to qiskit_to_tk conversion. #404

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from all 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
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:
Copy link
Contributor Author

@CalMacCQ CalMacCQ Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems more correct to match the optype as both of the qiskit instructions are mapped to a StatePreparationBox instance.

Copy link
Contributor Author

@CalMacCQ CalMacCQ Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact we could probably rewrite CircuitBuilder.add_qiskit_data to just check the optype and not look at the qiskit Instruction. This would be nicer as the whole method would just be matching on the values of the optype variable. Its just the handling of UnitaryGate and PauliEvolutionGate that are preventing this at the moment.

# 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