def test_sample_circuit_with_seed(): decomp = _simple_pauli_deco_dict(0.7, simplify_paulis=True) circ = Circuit([X.on(LineQubit(0)) for _ in range(10)]) expected = sample_circuit(circ, decomp, random_state=4)[0] # Check we're not sampling the same operation every call to sample_sequence assert len(set(expected.all_operations())) > 1 for _ in range(10): sampled = sample_circuit(circ, decomp, random_state=4)[0] assert _equal(sampled, expected)
def test_sample_circuit_random_state(seed, seed_type): decomposition = _simple_pauli_deco_dict(0.5) if isinstance(seed_type, np.random.RandomState): seed = np.random.RandomState(seed) circuit, sign, norm = sample_circuit(twoq_circ, decomposition, random_state=seed) for _ in range(20): if isinstance(seed_type, np.random.RandomState): seed = np.random.RandomState(seed) new_circuit, new_sign, new_norm = sample_circuit(twoq_circ, decomposition, random_state=seed) assert _equal(new_circuit, circuit) assert new_sign == sign assert np.isclose(new_norm, norm)
def test_sample_sequence_random_state(seed, seed_type, op): decomposition = _simple_pauli_deco_dict(0.5) if isinstance(seed_type, np.random.RandomState): seed = np.random.RandomState(seed) sequence, sign, norm = sample_sequence(op, decomposition, random_state=seed) for _ in range(20): if isinstance(seed_type, np.random.RandomState): seed = np.random.RandomState(seed) new_sequence, new_sign, new_norm = sample_sequence(op, decomposition, random_state=seed) assert new_sequence == sequence assert new_sign == sign assert np.isclose(new_norm, norm)
# You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. """Tests related to mitiq.pec.pec functions.""" from pytest import mark import numpy as np from cirq import Circuit, LineQubit, Y, Z, CNOT from mitiq.pec.utils import _simple_pauli_deco_dict, DecompositionDict from mitiq.pec.pec import execute_with_pec from mitiq.benchmarks.utils import noisy_simulation # The level of depolarizing noise for the simulated backend BASE_NOISE = 0.02 # Define some decomposition dictionaries for testing DECO_DICT = _simple_pauli_deco_dict(BASE_NOISE) DECO_DICT_SIMP = _simple_pauli_deco_dict(BASE_NOISE, simplify_paulis=True) NOISELESS_DECO_DICT = _simple_pauli_deco_dict(0) def executor(circuit: Circuit) -> float: """A one- or two-qubit noisy executor function. It executes the input circuit with BASE_NOISE depolarizing noise and returns the expectation value of the ground state projector. """ if len(circuit.all_qubits()) == 1: obs = np.array([[1, 0], [0, 0]]) elif len(circuit.all_qubits()) == 2: obs = np.array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) else: