Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotImplementedError when Circuit.unitary and Circuit.invert are called and circuit contains noise channels #1111

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -944,7 +949,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)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_models_circuit_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ def test_circuit_unitary_bigger(backend, with_measurement):
backend.assert_allclose(final_matrix, target_matrix)


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])
def test_circuit_vs_gate_execution(backend, compile):
"""Check consistency between executing circuit and stand alone gates."""
Expand Down