Skip to content

Commit

Permalink
Merge f27f74e into d6e4d52
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxiLi committed Sep 9, 2022
2 parents d6e4d52 + f27f74e commit 07654c2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/qutip_qip/circuit/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ def __init__(
"{{str: gate_function}}, not {}".format(user_gates)
)

def _create_empty_copy(self):
new_qc = QubitCircuit(
N=self.N,
input_states=self.input_states,
output_states=self.output_states,
reverse_states=self.reverse_states,
user_gates=self.user_gates,
dims=self.dims,
num_cbits=self.num_cbits,
)
return new_qc

def __copy__(self):
new_qc = self._create_empty_copy()
new_qc.gates = [gate for gate in self.gates]
return new_qc

def add_state(self, state, targets=None, state_type="input"):
"""
Add an input or ouput state to the circuit. By default all the input
Expand Down
15 changes: 15 additions & 0 deletions src/qutip_qip/operations/gateclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ def __init__(
if not all_integer:
raise ValueError("Index of a qubit must be an integer")

def __copy__(self):
new_gate = type(self).__new__(self.__class__)
new_gate.name = self.name
new_gate.targets = deepcopy(self.targets)
new_gate.controls = deepcopy(self.controls)
new_gate.classical_controls = deepcopy(self.classical_controls)
new_gate.classical_control_value = deepcopy(
self.classical_control_value
)
new_gate.control_value = deepcopy(self.control_value)
new_gate.arg_value = deepcopy(self.arg_value)
new_gate.arg_label = self.arg_label
new_gate.latex_str = self.latex_str
return new_gate

def get_all_qubits(self):
"""
Return a list of all qubits that the gate operator
Expand Down
13 changes: 5 additions & 8 deletions src/qutip_qip/transpiler/chain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from ..circuit import QubitCircuit


Expand Down Expand Up @@ -27,7 +28,7 @@ def to_chain_structure(qc, setup="linear"):
# FIXME This huge block has been here for a long time.
# It could be moved to the new compiler section and carefully
# splitted into smaller peaces.
qc_t = QubitCircuit(qc.N, qc.reverse_states)
qc_t = qc._create_empty_copy()
swap_gates = [
"SWAP",
"ISWAP",
Expand Down Expand Up @@ -265,12 +266,8 @@ def to_chain_structure(qc, setup="linear"):
j = j + 1

else:
qc_t.add_gate(
gate.name,
gate.targets,
gate.controls,
gate.arg_value,
gate.arg_label,
)
# This gate can be general quantum operations
# such as measurement or global phase.
qc_t.add_gate(gate)

return qc_t

0 comments on commit 07654c2

Please sign in to comment.