From 6e5c77f09b993f624ea86fbea57280bc0aa9d49a Mon Sep 17 00:00:00 2001 From: changsookim <> Date: Fri, 15 Nov 2024 16:30:46 +0400 Subject: [PATCH] int wire names & copy updat --- src/qibo/models/circuit.py | 24 ++++++++++++++---------- src/qibo/transpiler/optimizer.py | 7 +++++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/qibo/models/circuit.py b/src/qibo/models/circuit.py index 4e65259719..3e5c8a564e 100644 --- a/src/qibo/models/circuit.py +++ b/src/qibo/models/circuit.py @@ -171,13 +171,13 @@ def __init__( f"Number of qubits must be positive but is {nqubits}.", ) self.nqubits = nqubits - self.wire_names = wire_names self.init_kwargs = { "nqubits": nqubits, "accelerators": accelerators, "density_matrix": density_matrix, "wire_names": wire_names, } + self.wire_names = wire_names self.queue = _Queue(nqubits) # Keep track of parametrized gates for the ``set_parameters`` method self.parametrized_gates = _ParametrizedGates() @@ -315,10 +315,11 @@ def wire_names(self, wire_names: Union[list, dict]): if any([not isinstance(name, str) for name in wire_names.keys()]) or any( [not isinstance(name, str) for name in wire_names.values()] ): - raise_error( - ValueError, - "all keys and values in the ``wire_names`` dictionary must be type ``str``.", - ) + pass + # raise_error( + # ValueError, + # "all keys and values in the ``wire_names`` dictionary must be type ``str``.", + # ) self._wire_names = [ wire_names.get(f"q{i}", f"q{i}") for i in range(self.nqubits) @@ -326,6 +327,8 @@ def wire_names(self, wire_names: Union[list, dict]): else: self._wire_names = [f"q{i}" for i in range(self.nqubits)] + self.init_kwargs["wire_names"] = self._wire_names + @property def repeated_execution(self): return self.has_collapse or ( @@ -1282,6 +1285,7 @@ def diagram(self, line_wrap: int = 70, legend: bool = False) -> str: """Build the string representation of the circuit diagram.""" # build string representation of gates matrix = [[] for _ in range(self.nqubits)] + wire_names = [str(name) for name in self.wire_names] idx = [0] * self.nqubits for gate in self.queue: @@ -1303,12 +1307,12 @@ def diagram(self, line_wrap: int = 70, legend: bool = False) -> str: matrix[row][col] += "─" * (1 + maxlen - len(matrix[row][col])) # Print to terminal - max_name_len = max(len(name) for name in self.wire_names) + max_name_len = max(len(name) for name in wire_names) output = "" for q in range(self.nqubits): output += ( - self.wire_names[q] - + " " * (max_name_len - len(self.wire_names[q])) + wire_names[q] + + " " * (max_name_len - len(wire_names[q])) + ": ─" + "".join(matrix[q]) + "\n" @@ -1350,8 +1354,8 @@ def chunkstring(string, length): loutput += ["" for _ in range(self.nqubits)] suffix = " ...\n" prefix = ( - self.wire_names[row] - + " " * (max_name_len - len(self.wire_names[row])) + wire_names[row] + + " " * (max_name_len - len(wire_names[row])) + ": " ) if i == 0: diff --git a/src/qibo/transpiler/optimizer.py b/src/qibo/transpiler/optimizer.py index 7008957627..f8a3b10ada 100644 --- a/src/qibo/transpiler/optimizer.py +++ b/src/qibo/transpiler/optimizer.py @@ -27,7 +27,10 @@ def __call__(self, circuit: Circuit) -> Circuit: ) if logical_qubits == physical_qubits: return circuit - new_circuit = Circuit(physical_qubits) + new_wire_names = circuit.wire_names + list( + self.connectivity.nodes - circuit.wire_names + ) + new_circuit = Circuit(nqubits=physical_qubits, wire_names=new_wire_names) for gate in circuit.queue: new_circuit.add(gate) return new_circuit @@ -48,7 +51,7 @@ def __init__(self, max_qubits: int = 1): def __call__(self, circuit: Circuit): fused_circuit = circuit.fuse(max_qubits=self.max_qubits) - new = circuit.__class__(circuit.nqubits) + new = Circuit(**circuit.init_kwargs) for fgate in fused_circuit.queue: if isinstance(fgate, gates.FusedGate): new.add(gates.Unitary(fgate.matrix(), *fgate.qubits))