Skip to content

Commit

Permalink
int wire names & copy updat
Browse files Browse the repository at this point in the history
  • Loading branch information
changsookim committed Nov 15, 2024
1 parent faa5a7c commit 6e5c77f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -315,17 +315,20 @@ 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)
]
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 (
Expand Down Expand Up @@ -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:
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions src/qibo/transpiler/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 6e5c77f

Please sign in to comment.