Skip to content

Commit

Permalink
Add __copy__ for Gate and QubitCircuit
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxiLi committed Sep 9, 2022
1 parent 7ebec1a commit f27f74e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 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
3 changes: 1 addition & 2 deletions src/qutip_qip/transpiler/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +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 = deepcopy(qc)
qc_t.gates = []
qc_t = qc._create_empty_copy()
swap_gates = [
"SWAP",
"ISWAP",
Expand Down

0 comments on commit f27f74e

Please sign in to comment.