From 7d76ecd1dcab28becb3b7905eb882d326e2046da Mon Sep 17 00:00:00 2001 From: simone bordoni Date: Wed, 7 Feb 2024 15:26:36 +0400 Subject: [PATCH 1/2] rename qubits based on connectivity and solved minor bugs --- src/qibo/transpiler/blocks.py | 4 ++-- src/qibo/transpiler/router.py | 8 ++++++-- src/qibo/transpiler/unroller.py | 2 +- tests/test_transpiler_pipeline.py | 1 + tests/test_transpiler_router.py | 1 + 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/qibo/transpiler/blocks.py b/src/qibo/transpiler/blocks.py index 199979767e..5b881d9e06 100644 --- a/src/qibo/transpiler/blocks.py +++ b/src/qibo/transpiler/blocks.py @@ -65,8 +65,8 @@ def fuse(self, block, name: Optional[str] = None): (:class:`qibo.transpiler.blocks.Block`): fusion of the two input blocks. """ if not self.qubits == block.qubits: - raise_error( - BlockingError, "In order to fuse two blocks their qubits must coincide." + raise BlockingError( + "In order to fuse two blocks their qubits must coincide." ) return Block(qubits=self.qubits, gates=self.gates + block.gates, name=name) diff --git a/src/qibo/transpiler/router.py b/src/qibo/transpiler/router.py index 8a3bf54180..6023296d8c 100644 --- a/src/qibo/transpiler/router.py +++ b/src/qibo/transpiler/router.py @@ -379,7 +379,9 @@ def __call__(self, circuit: Circuit, initial_layout: dict): else: self._find_new_mapping() - routed_circuit = self.circuit.routed_circuit(circuit_kwargs=circuit.init_kwargs) + circuit_kwargs = circuit.init_kwargs + circuit_kwargs["wire_names"] = list(initial_layout.keys()) + routed_circuit = self.circuit.routed_circuit(circuit_kwargs=circuit_kwargs) if self._final_measurements is not None: routed_circuit = self._append_final_measurements( routed_circuit=routed_circuit @@ -658,7 +660,9 @@ def __call__(self, circuit: Circuit, initial_layout: dict): else: self._find_new_mapping() - routed_circuit = self.circuit.routed_circuit(circuit_kwargs=circuit.init_kwargs) + circuit_kwargs = circuit.init_kwargs + circuit_kwargs["wire_names"] = list(initial_layout.keys()) + routed_circuit = self.circuit.routed_circuit(circuit_kwargs=circuit_kwargs) if self._final_measurements is not None: routed_circuit = self._append_final_measurements( routed_circuit=routed_circuit diff --git a/src/qibo/transpiler/unroller.py b/src/qibo/transpiler/unroller.py index c62ae4e273..6b8f3e9c60 100644 --- a/src/qibo/transpiler/unroller.py +++ b/src/qibo/transpiler/unroller.py @@ -85,7 +85,7 @@ def __call__(self, circuit: Circuit): Returns: (:class:`qibo.models.circuit.Circuit`): equivalent circuit with native gates. """ - translated_circuit = circuit.__class__(circuit.nqubits) + translated_circuit = Circuit(**circuit.init_kwargs) for gate in circuit.queue: translated_circuit.add( translate_gate( diff --git a/tests/test_transpiler_pipeline.py b/tests/test_transpiler_pipeline.py index ce8afee66d..94d2c4b539 100644 --- a/tests/test_transpiler_pipeline.py +++ b/tests/test_transpiler_pipeline.py @@ -237,6 +237,7 @@ def test_custom_passes_restict(gates, placer, routing): final_layout=final_layout, native_gates=NativeGates.default(), ) + assert transpiled_circ.wire_names == ["q1", "q2", "q3"] def test_custom_passes_multiple_placer(): diff --git a/tests/test_transpiler_router.py b/tests/test_transpiler_router.py index 2cf2364fe9..7a7ef17583 100644 --- a/tests/test_transpiler_router.py +++ b/tests/test_transpiler_router.py @@ -348,6 +348,7 @@ def test_restrict_qubits(router_algorithm): ) assert_connectivity(restricted_connectivity, routed_circ) assert_placement(routed_circ, final_layout, connectivity=restricted_connectivity) + assert routed_circ.wire_names == ["q0", "q2", "q3"] def test_star_error_multi_qubit(): From ee34da2f25e2d0a2b9f20b48a911c7982a18a699 Mon Sep 17 00:00:00 2001 From: simone bordoni Date: Wed, 14 Feb 2024 14:58:39 +0400 Subject: [PATCH 2/2] avoid raise_error in block decomposition --- src/qibo/transpiler/blocks.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/qibo/transpiler/blocks.py b/src/qibo/transpiler/blocks.py index 5b881d9e06..6e2b1122a2 100644 --- a/src/qibo/transpiler/blocks.py +++ b/src/qibo/transpiler/blocks.py @@ -65,8 +65,8 @@ def fuse(self, block, name: Optional[str] = None): (:class:`qibo.transpiler.blocks.Block`): fusion of the two input blocks. """ if not self.qubits == block.qubits: - raise BlockingError( - "In order to fuse two blocks their qubits must coincide." + raise_error( + BlockingError, "In order to fuse two blocks their qubits must coincide." ) return Block(qubits=self.qubits, gates=self.gates + block.gates, name=name) @@ -200,12 +200,11 @@ def block_decomposition(circuit: Circuit, fuse: bool = True): remove_list = [first_block] if len(initial_blocks[1:]) > 0: for second_block in initial_blocks[1:]: - try: + if second_block.qubits == first_block.qubits: first_block = first_block.fuse(second_block) remove_list.append(second_block) - except BlockingError: - if not first_block.commute(second_block): - break + elif not first_block.commute(second_block): + break blocks.append(first_block) _remove_gates(initial_blocks, remove_list)