From 81e9df4fd84fbec18123eb91cdda20e3e3937211 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 29 Nov 2023 14:32:39 +0400 Subject: [PATCH 1/5] error --- src/qibo/models/circuit.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/qibo/models/circuit.py b/src/qibo/models/circuit.py index 8a2a1aa6e5..b59951869e 100644 --- a/src/qibo/models/circuit.py +++ b/src/qibo/models/circuit.py @@ -944,7 +944,12 @@ def unitary(self, backend=None): fgate = gates.FusedGate(*range(self.nqubits)) for gate in self.queue: - if not isinstance(gate, (gates.SpecialGate, gates.M)): + if isinstance(gate, gates.Channel): + raise_error( + NotImplementedError, + "`unitary` method not implemented for circuits that contain noise channels." + ) + elif not isinstance(gate, (gates.SpecialGate, gates.M)): fgate.append(gate) return fgate.matrix(backend) From 1f6d28a07e594527280c9c27c1396b3fb75f961b Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 29 Nov 2023 14:32:45 +0400 Subject: [PATCH 2/5] test --- tests/test_models_circuit_features.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_models_circuit_features.py b/tests/test_models_circuit_features.py index 99b45b74c7..ee3030025c 100644 --- a/tests/test_models_circuit_features.py +++ b/tests/test_models_circuit_features.py @@ -45,6 +45,15 @@ def test_circuit_unitary_bigger(backend, with_measurement): backend.assert_allclose(final_matrix, target_matrix) +def test_circuit_unitary_with_noise_channel(backend): + circuit = Circuit(2) + circuit.add(gates.H(0)) + circuit.add(gates.CNOT(0, 1)) + circuit.add(gates.DepolarizingChannel([0, 1], 0.2)) + with pytest.raises(NotImplementedError): + circuit.unitary(backend) + + @pytest.mark.parametrize("compile", [False, True]) def test_circuit_vs_gate_execution(backend, compile): """Check consistency between executing circuit and stand alone gates.""" From 461c3ffc7ad9778d0f8267f95f534c9ba39edca7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:34:55 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/qibo/models/circuit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qibo/models/circuit.py b/src/qibo/models/circuit.py index b59951869e..45809e0528 100644 --- a/src/qibo/models/circuit.py +++ b/src/qibo/models/circuit.py @@ -946,8 +946,8 @@ def unitary(self, backend=None): for gate in self.queue: if isinstance(gate, gates.Channel): raise_error( - NotImplementedError, - "`unitary` method not implemented for circuits that contain noise channels." + NotImplementedError, + "`unitary` method not implemented for circuits that contain noise channels.", ) elif not isinstance(gate, (gates.SpecialGate, gates.M)): fgate.append(gate) From ddc5fbe66eb81fb0f4b8654c8528ffeceb9e3090 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 29 Nov 2023 14:49:49 +0400 Subject: [PATCH 4/5] invert --- src/qibo/models/circuit.py | 7 ++++++- tests/test_models_circuit_features.py | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/qibo/models/circuit.py b/src/qibo/models/circuit.py index 45809e0528..6d515cf715 100644 --- a/src/qibo/models/circuit.py +++ b/src/qibo/models/circuit.py @@ -405,7 +405,12 @@ def invert(self): measurements = [] new_circuit = self.__class__(**self.init_kwargs) for gate in self.queue[::-1]: - if isinstance(gate, gates.M) and skip_measurements: + if isinstance(gate, gates.Channel): + raise_error( + NotImplementedError, + "`invert` method not implemented for circuits that contain noise channels." + ) + elif isinstance(gate, gates.M) and skip_measurements: measurements.append(gate) else: new_gate = gate.dagger() diff --git a/tests/test_models_circuit_features.py b/tests/test_models_circuit_features.py index ee3030025c..8944b25b6a 100644 --- a/tests/test_models_circuit_features.py +++ b/tests/test_models_circuit_features.py @@ -45,13 +45,15 @@ def test_circuit_unitary_bigger(backend, with_measurement): backend.assert_allclose(final_matrix, target_matrix) -def test_circuit_unitary_with_noise_channel(backend): +def test_circuit_unitary_and_inverse_with_noise_channel(backend): circuit = Circuit(2) circuit.add(gates.H(0)) circuit.add(gates.CNOT(0, 1)) circuit.add(gates.DepolarizingChannel([0, 1], 0.2)) with pytest.raises(NotImplementedError): circuit.unitary(backend) + with pytest.raises(NotImplementedError): + circuit.invert() @pytest.mark.parametrize("compile", [False, True]) From efdf7cf3c8c4ea1a3244be98fd4e05fb6f3bd2cd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:50:13 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/qibo/models/circuit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/models/circuit.py b/src/qibo/models/circuit.py index 6d515cf715..bd92eadfbb 100644 --- a/src/qibo/models/circuit.py +++ b/src/qibo/models/circuit.py @@ -408,7 +408,7 @@ def invert(self): if isinstance(gate, gates.Channel): raise_error( NotImplementedError, - "`invert` method not implemented for circuits that contain noise channels." + "`invert` method not implemented for circuits that contain noise channels.", ) elif isinstance(gate, gates.M) and skip_measurements: measurements.append(gate)