diff --git a/doc/source/code-examples/advancedexamples.rst b/doc/source/code-examples/advancedexamples.rst index e7ca572b39..29e889ec44 100644 --- a/doc/source/code-examples/advancedexamples.rst +++ b/doc/source/code-examples/advancedexamples.rst @@ -999,7 +999,7 @@ Here is an example on how to use a noise model: noise.add(PauliError([("X", 0.5)]), gates.H, 1) noise.add(PauliError([("Y", 0.5)]), gates.CNOT) is_sqrt_x = (lambda g: np.pi/2 in g.parameters) - noise.add(PauliError([("X", 0.5)]), gates.RX, qubits=0, condition=is_sqrt_x) + noise.add(PauliError([("X", 0.5)]), gates.RX, qubits=0, conditions=is_sqrt_x) # Generate noiseless circuit. c = models.Circuit(2) diff --git a/src/qibo/noise.py b/src/qibo/noise.py index 2fac444d4e..d0afe8529d 100644 --- a/src/qibo/noise.py +++ b/src/qibo/noise.py @@ -260,7 +260,7 @@ def is_sqrt_x(gate): # Build a noise model with a Pauli error on RX(pi/2) gates. error = PauliError(list(zip(["X", "Y", "Z"], [0.01, 0.5, 0.1]))) noise = NoiseModel() - noise.add(PauliError([("X", 0.5)]), gates.RX, condition=is_sqrt_x) + noise.add(PauliError([("X", 0.5)]), gates.RX, conditions=is_sqrt_x) # Generate a noiseless circuit. circuit = Circuit(1) @@ -328,7 +328,11 @@ def apply(self, circuit): for _, error, qubits in errors_list if isinstance(error, ReadoutError) ] - if gate.qubits not in readout_error_qubits: + if ( + gate.qubits not in readout_error_qubits + and gate.register_name + not in noisy_circuit.measurement_tuples.keys() + ): noisy_circuit.add(gate) for conditions, error, qubits in errors_list: diff --git a/tests/test_backends_clifford.py b/tests/test_backends_clifford.py index 13d5ef3aa9..de5c8a0b68 100644 --- a/tests/test_backends_clifford.py +++ b/tests/test_backends_clifford.py @@ -22,10 +22,11 @@ def construct_clifford_backend(backend): clifford_backend = CliffordBackend(backend) assert ( str(excinfo.value) - == "TensorflowBackend for Clifford Simulation is not supported yet." + == "TensorflowBackend for Clifford Simulation is not supported." ) - else: - return CliffordBackend(backend) + pytest.skip("Clifford backend not defined for the tensorflow engine.") + + return CliffordBackend(backend) THETAS_1Q = [ @@ -38,13 +39,10 @@ def construct_clifford_backend(backend): @pytest.mark.parametrize("axis,theta", list(product(AXES, THETAS_1Q))) def test_rotations_1q(backend, theta, axis): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = Circuit(3, density_matrix=True) qubits = np.random.randint(3, size=2) H_qubits = np.random.choice(range(3), size=2, replace=False) - for q in H_qubits: - c.add(gates.H(q)) + c.add(gates.H(q) for q in H_qubits) c.add(getattr(gates, axis)(qubits[0], theta=theta)) c.add(getattr(gates, axis)(qubits[1], theta=theta)) clifford_state = clifford_bkd.execute_circuit(c).state() @@ -59,14 +57,11 @@ def test_rotations_1q(backend, theta, axis): @pytest.mark.parametrize("axis,theta", list(product(AXES, THETAS_2Q))) def test_rotations_2q(backend, theta, axis): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = Circuit(3, density_matrix=True) qubits_0 = np.random.choice(range(3), size=2, replace=False) qubits_1 = np.random.choice(range(3), size=2, replace=False) H_qubits = np.random.choice(range(3), size=2, replace=False) - for q in H_qubits: - c.add(gates.H(q)) + c.add(gates.H(q) for q in H_qubits) c.add(getattr(gates, f"C{axis}")(*qubits_0, theta=theta)) c.add(getattr(gates, f"C{axis}")(*qubits_1, theta=theta)) clifford_state = clifford_bkd.execute_circuit(c).state() @@ -81,13 +76,10 @@ def test_rotations_2q(backend, theta, axis): @pytest.mark.parametrize("gate", SINGLE_QUBIT_CLIFFORDS) def test_single_qubit_gates(backend, gate): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = Circuit(3, density_matrix=True) qubits = np.random.randint(3, size=2) H_qubits = np.random.choice(range(3), size=2, replace=False) - for q in H_qubits: - c.add(gates.H(q)) + c.add(gates.H(q) for q in H_qubits) c.add(getattr(gates, gate)(qubits[0])) c.add(getattr(gates, gate)(qubits[1])) clifford_state = clifford_bkd.execute_circuit(c).state() @@ -102,13 +94,10 @@ def test_single_qubit_gates(backend, gate): @pytest.mark.parametrize("gate", TWO_QUBITS_CLIFFORDS) def test_two_qubits_gates(backend, gate): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = Circuit(5, density_matrix=True) qubits = np.random.choice(range(5), size=4, replace=False).reshape(2, 2) H_qubits = np.random.choice(range(5), size=3, replace=False) - for q in H_qubits: - c.add(gates.H(q)) + c.add(gates.H(q) for q in H_qubits) c.add(getattr(gates, gate)(*qubits[0])) c.add(getattr(gates, gate)(*qubits[1])) clifford_state = clifford_bkd.execute_circuit(c).state() @@ -135,8 +124,6 @@ def test_two_qubits_gates(backend, gate): ) def test_random_clifford_circuit(backend, prob_qubits, binary): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = random_clifford(5, seed=1, backend=backend) c.density_matrix = True c_copy = c.copy() @@ -164,20 +151,17 @@ def test_random_clifford_circuit(backend, prob_qubits, binary): atol=1e-1, ) + nshots = 1000 numpy_freq = numpy_result.frequencies(binary) clifford_freq = clifford_result.frequencies(binary) clifford_freq = {state: clifford_freq[state] for state in numpy_freq.keys()} assert len(numpy_freq) == len(clifford_freq) for np_count, clif_count in zip(numpy_freq.values(), clifford_freq.values()): - backend.assert_allclose( - np_count / 1000, clif_count / 1000, atol=1e-1 - ) # nshots = 1000 + backend.assert_allclose(np_count / nshots, clif_count / nshots, atol=1e-1) def test_collapsing_measurements(backend): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return gate_queue = random_clifford(3, density_matrix=True, backend=backend).queue measured_qubits = np.random.choice(range(3), size=2, replace=False) c1 = Circuit(3) @@ -200,8 +184,6 @@ def test_collapsing_measurements(backend): def test_non_clifford_error(backend): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = Circuit(1) c.add(gates.T(0)) with pytest.raises(RuntimeError) as excinfo: @@ -211,8 +193,6 @@ def test_non_clifford_error(backend): def test_initial_state(backend): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return state = random_clifford(3, backend=numpy_bkd) tmp = clifford_bkd.execute_circuit(state) initial_symplectic_matrix = tmp.symplectic_matrix @@ -228,8 +208,6 @@ def test_initial_state(backend): def test_bitflip_noise(backend): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = random_clifford(5, backend=backend) c_copy = c.copy() qubits = np.random.choice(range(3), size=2, replace=False) @@ -244,8 +222,6 @@ def test_bitflip_noise(backend): def test_set_backend(backend): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return platform = backend.platform if platform is None: platform = str(backend) @@ -255,8 +231,6 @@ def test_set_backend(backend): def test_noise_channels(backend): clifford_bkd = construct_clifford_backend(backend) - if not clifford_bkd: - return c = random_clifford(5, backend=backend) c.density_matrix = True c_copy = c.copy() diff --git a/tests/test_models_circuit_features.py b/tests/test_models_circuit_features.py index d0e6849145..9f36476ce7 100644 --- a/tests/test_models_circuit_features.py +++ b/tests/test_models_circuit_features.py @@ -323,15 +323,17 @@ def test_repeated_execute_probs_and_freqs(backend, nqubits): # Tensorflow seems to yield different results with same seed if backend.__class__.__name__ == "TensorflowBackend": - if nqubits == 1: - test_frequencies = Counter({"1": 844, "0": 180}) - else: - test_frequencies = Counter({"11": 674, "10": 155, "01": 154, "00": 41}) + test_frequencies = ( + Counter({"1": 801, "0": 223}) + if nqubits == 1 + else Counter({"11": 662, "10": 163, "01": 160, "00": 39}) + ) else: - if nqubits == 1: - test_frequencies = Counter({"1": 790, "0": 234}) - else: - test_frequencies = Counter({"11": 618, "10": 169, "01": 185, "00": 52}) + test_frequencies = ( + Counter({"1": 790, "0": 234}) + if nqubits == 1 + else Counter({"11": 618, "10": 169, "01": 185, "00": 52}) + ) for key in dict(test_frequencies).keys(): backend.assert_allclose(result.frequencies()[key], test_frequencies[key]) diff --git a/tests/test_noise.py b/tests/test_noise.py index 5efc492930..19e21bd14b 100644 --- a/tests/test_noise.py +++ b/tests/test_noise.py @@ -530,11 +530,11 @@ def condition_3_pi_2(gate): reset = ResetError(0.8, 0.2) thermal = ThermalRelaxationError(2, 1, 0.3) noise = NoiseModel() - noise.add(reset, gates.RX, condition=condition_pi_2) - noise.add(thermal, gates.RX, condition=condition_3_pi_2) + noise.add(reset, gates.RX, conditions=condition_pi_2) + noise.add(thermal, gates.RX, conditions=condition_3_pi_2) with pytest.raises(TypeError): - noise.add(reset, gates.RX, condition=2) + noise.add(reset, gates.RX, conditions=2) circuit = Circuit(3, density_matrix=density_matrix) circuit.add(gates.RX(0, np.pi))