def get_pauli_and_cnot_representations( base_noise: float, qubits: Optional[List[cirq.Qid]] = None, ) -> List[OperationRepresentation]: if qubits is None: qreg = cirq.LineQubit.range(2) else: qreg = qubits # Generate all ideal single-qubit Pauli operations for both qubits pauli_gates = [cirq.X, cirq.Y, cirq.Z] ideal_operations = [] for gate in pauli_gates: for qubit in qreg: ideal_operations.append(gate(qubit)) # Add CNOT operation too ideal_operations.append(cirq.CNOT(*qreg)) # Generate all representations return represent_operations_in_circuit_with_local_depolarizing_noise( ideal_circuit=cirq.Circuit(ideal_operations), noise_level=base_noise, )
def test_represent_operations_in_circuit_local(circuit_type: str): """Tests all operation representations are created.""" qreg = LineQubit.range(2) circ_mitiq = Circuit([CNOT(*qreg), H(qreg[0]), Y(qreg[1]), CNOT(*qreg)]) circ = convert_from_mitiq(circ_mitiq, circuit_type) reps = represent_operations_in_circuit_with_local_depolarizing_noise( ideal_circuit=circ, noise_level=0.1, ) for op in convert_to_mitiq(circ)[0].all_operations(): found = False for rep in reps: if _equal(rep.ideal, Circuit(op), require_qubit_equality=True): found = True assert found # The number of reps. should match the number of unique operations assert len(reps) == 3
def test_sample_circuit_choi(): """Tests the sample_circuit by comparing the exact Choi matrices.""" # A simple 2-qubit circuit qreg = cirq.LineQubit.range(2) ideal_circ = cirq.Circuit( cirq.X.on(qreg[0]), cirq.I.on(qreg[1]), cirq.CNOT.on(*qreg), ) noisy_circuit = ideal_circ.with_noise(cirq.depolarize(BASE_NOISE)) ideal_choi = _circuit_to_choi(ideal_circ) noisy_choi = _operation_to_choi(noisy_circuit) rep_list = represent_operations_in_circuit_with_local_depolarizing_noise( ideal_circuit=ideal_circ, noise_level=BASE_NOISE, ) choi_unbiased_estimates = [] rng = np.random.RandomState(1) for _ in range(500): imp_circs, signs, norm = sample_circuit(ideal_circ, rep_list, random_state=rng) noisy_imp_circ = imp_circs[0].with_noise(cirq.depolarize(BASE_NOISE)) sequence_choi = _circuit_to_choi(noisy_imp_circ) choi_unbiased_estimates.append(norm * signs[0] * sequence_choi) choi_pec_estimate = np.average(choi_unbiased_estimates, axis=0) noise_error = np.linalg.norm(ideal_choi - noisy_choi) pec_error = np.linalg.norm(ideal_choi - choi_pec_estimate) assert pec_error < noise_error assert np.allclose(ideal_choi, choi_pec_estimate, atol=0.05)