def test_two_qubit_gates(op): q0, q1 = cirq.LineQubit.range(2) props = TestNoiseProperties( **default_props([q0, q1], [(q0, q1), (q1, q0)])) model = NoiseModelFromNoiseProperties(props) circuit = cirq.Circuit(op) noisy_circuit = circuit.with_noise(model) assert len(noisy_circuit.moments) == 3 assert len(noisy_circuit.moments[0].operations) == 1 assert noisy_circuit.moments[0].operations[0] == op.with_tags( PHYSICAL_GATE_TAG) # Depolarizing noise assert len(noisy_circuit.moments[1].operations) == 1 depol_op = noisy_circuit.moments[1].operations[0] assert isinstance(depol_op.gate, cirq.DepolarizingChannel) assert np.isclose(depol_op.gate.p, 0.00952008) # Thermal noise assert len(noisy_circuit.moments[2].operations) == 2 thermal_op_0 = noisy_circuit.moments[2].operation_at(q0) thermal_op_1 = noisy_circuit.moments[2].operation_at(q1) assert isinstance(thermal_op_0.gate, cirq.KrausChannel) assert isinstance(thermal_op_1.gate, cirq.KrausChannel) thermal_choi_0 = cirq.kraus_to_choi(cirq.kraus(thermal_op_0)) thermal_choi_1 = cirq.kraus_to_choi(cirq.kraus(thermal_op_1)) expected_thermal_choi = np.array([ [1, 0, 0, 9.99680051e-01], [0, 3.19948805e-04, 0, 0], [0, 0, 0, 0], [9.99680051e-01, 0, 0, 9.99680051e-01], ]) assert np.allclose(thermal_choi_0, expected_thermal_choi) assert np.allclose(thermal_choi_1, expected_thermal_choi)
def test_noisy_moment_two_qubit(): q0, q1 = cirq.LineQubit.range(2) model = ThermalNoiseModel( qubits={q0, q1}, gate_durations_ns={cirq.PhasedXZGate: 25.0, cirq.CZPowGate: 25.0}, heat_rate_GHz={q0: 1e-5, q1: 2e-5}, cool_rate_GHz={q0: 1e-4, q1: 2e-4}, dephase_rate_GHz={q0: 3e-4, q1: 4e-4}, require_physical_tag=False, ) gate = cirq.CZ**0.5 moment = cirq.Moment(gate.on(q0, q1)) noisy_moment = model.noisy_moment(moment, system_qubits=[q0, q1]) assert noisy_moment[0] == moment assert len(noisy_moment[1]) == 2 noisy_choi_0 = cirq.kraus_to_choi(cirq.kraus(noisy_moment[1].operations[0])) assert np.allclose( noisy_choi_0, [ [9.99750343e-01, 0, 0, 9.91164267e-01], [0, 2.49656565e-03, 0, 0], [0, 0, 2.49656565e-04, 0], [9.91164267e-01, 0, 0, 9.97503434e-01], ], ) noisy_choi_1 = cirq.kraus_to_choi(cirq.kraus(noisy_moment[1].operations[1])) assert np.allclose( noisy_choi_1, [ [9.99501372e-01, 0, 0, 9.87330937e-01], [0, 4.98627517e-03, 0, 0], [0, 0, 4.98627517e-04, 0], [9.87330937e-01, 0, 0, 9.95013725e-01], ], )
def test_two_qubit_gates(op): q0, q1 = cirq.LineQubit.range(2) props = sample_noise_properties([q0, q1], [(q0, q1), (q1, q0)]) model = NoiseModelFromGoogleNoiseProperties(props) circuit = cirq.Circuit(op) noisy_circuit = circuit.with_noise(model) assert len(noisy_circuit.moments) == 4 assert len(noisy_circuit.moments[0].operations) == 1 assert noisy_circuit.moments[0].operations[0] == op.with_tags( PHYSICAL_GATE_TAG) # Depolarizing noise assert len(noisy_circuit.moments[1].operations) == 1 depol_op = noisy_circuit.moments[1].operations[0] assert isinstance(depol_op.gate, cirq.DepolarizingChannel) assert np.isclose(depol_op.gate.p, 0.00719705) # FSim angle corrections assert len(noisy_circuit.moments[2].operations) == 1 fsim_op = noisy_circuit.moments[2].operations[0] assert isinstance(fsim_op.gate, cirq.PhasedFSimGate) assert fsim_op == PhasedFSimGate(theta=0.01, zeta=0.03, chi=0.04, gamma=0.05, phi=0.02).on(q0, q1) # Thermal noise assert len(noisy_circuit.moments[3].operations) == 2 thermal_op_0 = noisy_circuit.moments[3].operation_at(q0) thermal_op_1 = noisy_circuit.moments[3].operation_at(q1) assert isinstance(thermal_op_0.gate, cirq.KrausChannel) assert isinstance(thermal_op_1.gate, cirq.KrausChannel) thermal_choi_0 = cirq.kraus_to_choi(cirq.kraus(thermal_op_0)) thermal_choi_1 = cirq.kraus_to_choi(cirq.kraus(thermal_op_1)) expected_thermal_choi = np.array([ [1, 0, 0, 9.99680051e-01], [0, 3.19948805e-04, 0, 0], [0, 0, 0, 0], [9.99680051e-01, 0, 0, 9.99680051e-01], ]) assert np.allclose(thermal_choi_0, expected_thermal_choi) assert np.allclose(thermal_choi_1, expected_thermal_choi) # Pauli error for depol_op + fsim_op + thermal_op_(0|1) == total (0.01) depol_pauli_err = 1 - cirq.qis.measures.entanglement_fidelity(depol_op) fsim_pauli_err = 1 - cirq.qis.measures.entanglement_fidelity(fsim_op) thermal0_pauli_err = 1 - cirq.qis.measures.entanglement_fidelity( thermal_op_0) thermal1_pauli_err = 1 - cirq.qis.measures.entanglement_fidelity( thermal_op_1) total_err = depol_pauli_err + thermal0_pauli_err + thermal1_pauli_err + fsim_pauli_err assert np.isclose(total_err, TWO_QUBIT_ERROR)
def test_wait_gates(): q0 = cirq.LineQubit(0) props = sample_noise_properties([q0], []) model = NoiseModelFromGoogleNoiseProperties(props) op = cirq.wait(q0, nanos=100) circuit = cirq.Circuit(op) noisy_circuit = circuit.with_noise(model) assert len(noisy_circuit.moments) == 2 assert noisy_circuit.moments[0].operations[0] == op.with_tags(PHYSICAL_GATE_TAG) # No depolarizing noise because WaitGate has none. assert len(noisy_circuit.moments[1].operations) == 1 thermal_op = noisy_circuit.moments[1].operations[0] assert isinstance(thermal_op.gate, cirq.KrausChannel) thermal_choi = cirq.kraus_to_choi(cirq.kraus(thermal_op)) assert np.allclose( thermal_choi, [ [1, 0, 0, 9.990005e-01], [0, 9.99500167e-04, 0, 0], [0, 0, 0, 0], [9.990005e-01, 0, 0, 9.990005e-01], ], )
def test_noise_from_wait(): # Verify that wait-gate noise is duration-dependent. q0 = cirq.LineQubit(0) gate_durations = {cirq.ZPowGate: 25.0} heat_rate_GHz = {q0: 1e-5} cool_rate_GHz = {q0: 1e-4} model = ThermalNoiseModel( qubits={q0}, gate_durations_ns=gate_durations, heat_rate_GHz=heat_rate_GHz, cool_rate_GHz=cool_rate_GHz, dephase_rate_GHz=None, require_physical_tag=False, skip_measurements=True, ) moment = cirq.Moment(cirq.wait(q0, nanos=100)) noisy_moment = model.noisy_moment(moment, system_qubits=[q0]) assert noisy_moment[0] == moment assert len(noisy_moment[1]) == 1 noisy_choi = cirq.kraus_to_choi(cirq.kraus(noisy_moment[1].operations[0])) print(noisy_choi) assert np.allclose( noisy_choi, [ [9.99005480e-01, 0, 0, 9.94515097e-01], [0, 9.94520111e-03, 0, 0], [0, 0, 9.94520111e-04, 0], [9.94515097e-01, 0, 0, 9.90054799e-01], ], )
def test_single_qubit_gates(op): q0 = cirq.LineQubit(0) props = TestNoiseProperties(**default_props([q0], [])) model = NoiseModelFromNoiseProperties(props) circuit = cirq.Circuit(op) noisy_circuit = circuit.with_noise(model) assert len(noisy_circuit.moments) == 3 assert len(noisy_circuit.moments[0].operations) == 1 assert noisy_circuit.moments[0].operations[0] == op.with_tags( PHYSICAL_GATE_TAG) # Depolarizing noise assert len(noisy_circuit.moments[1].operations) == 1 depol_op = noisy_circuit.moments[1].operations[0] assert isinstance(depol_op.gate, cirq.DepolarizingChannel) assert np.isclose(depol_op.gate.p, 0.00081252) # Thermal noise assert len(noisy_circuit.moments[2].operations) == 1 thermal_op = noisy_circuit.moments[2].operations[0] assert isinstance(thermal_op.gate, cirq.KrausChannel) thermal_choi = cirq.kraus_to_choi(cirq.kraus(thermal_op)) assert np.allclose( thermal_choi, [ [1, 0, 0, 9.99750031e-01], [0, 2.49968753e-04, 0, 0], [0, 0, 0, 0], [9.99750031e-01, 0, 0, 9.99750031e-01], ], )
def test_noisy_moment_one_qubit(): q0, q1 = cirq.LineQubit.range(2) model = ThermalNoiseModel( qubits={q0, q1}, gate_durations_ns={cirq.PhasedXZGate: 25.0, cirq.CZPowGate: 25.0}, heat_rate_GHz={q0: 1e-5, q1: 2e-5}, cool_rate_GHz={q0: 1e-4, q1: 2e-4}, dephase_rate_GHz={q0: 3e-4, q1: 4e-4}, require_physical_tag=False, ) gate = cirq.PhasedXZGate(x_exponent=1, z_exponent=0.5, axis_phase_exponent=0.25) moment = cirq.Moment(gate.on(q0)) noisy_moment = model.noisy_moment(moment, system_qubits=[q0, q1]) assert noisy_moment[0] == moment # Noise applies to both qubits, even if only one is acted upon. assert len(noisy_moment[1]) == 2 noisy_choi = cirq.kraus_to_choi(cirq.kraus(noisy_moment[1].operations[0])) assert np.allclose( noisy_choi, [ [9.99750343e-01, 0, 0, 9.91164267e-01], [0, 2.49656565e-03, 0, 0], [0, 0, 2.49656565e-04, 0], [9.91164267e-01, 0, 0, 9.97503434e-01], ], )
def test_single_qubit_gates(op): q0 = cirq.LineQubit(0) props = sample_noise_properties([q0], []) model = NoiseModelFromGoogleNoiseProperties(props) circuit = cirq.Circuit(op) noisy_circuit = circuit.with_noise(model) assert len(noisy_circuit.moments) == 3 assert len(noisy_circuit.moments[0].operations) == 1 assert noisy_circuit.moments[0].operations[0] == op.with_tags( PHYSICAL_GATE_TAG) # Depolarizing noise assert len(noisy_circuit.moments[1].operations) == 1 depol_op = noisy_circuit.moments[1].operations[0] assert isinstance(depol_op.gate, cirq.DepolarizingChannel) assert np.isclose(depol_op.gate.p, 0.00081252) # Thermal noise assert len(noisy_circuit.moments[2].operations) == 1 thermal_op = noisy_circuit.moments[2].operations[0] assert isinstance(thermal_op.gate, cirq.KrausChannel) thermal_choi = cirq.kraus_to_choi(cirq.kraus(thermal_op)) assert np.allclose( thermal_choi, [ [1, 0, 0, 9.99750031e-01], [0, 2.49968753e-04, 0, 0], [0, 0, 0, 0], [9.99750031e-01, 0, 0, 9.99750031e-01], ], ) # Pauli error for depol_op + thermal_op == total (0.001) depol_pauli_err = 1 - cirq.qis.measures.entanglement_fidelity(depol_op) thermal_pauli_err = 1 - cirq.qis.measures.entanglement_fidelity(thermal_op) total_err = depol_pauli_err + thermal_pauli_err assert np.isclose(total_err, SINGLE_QUBIT_ERROR)
def test_kraus_to_choi(kraus_operators, expected_choi): """Verifies that cirq.kraus_to_choi computes the correct Choi matrix.""" assert np.allclose(cirq.kraus_to_choi(kraus_operators), expected_choi)
def test_choi_to_kraus_atol(): """Verifies that insignificant Kraus operators are omitted.""" choi = cirq.kraus_to_choi(cirq.kraus(cirq.phase_damp(1e-6))) assert len(cirq.choi_to_kraus(choi, atol=1e-2)) == 1 assert len(cirq.choi_to_kraus(choi, atol=1e-4)) == 2
def test_choi_to_kraus_inverse_of_kraus_to_choi(choi): """Verifies that cirq.kraus_to_choi(cirq.choi_to_kraus(.)) is identity on Choi matrices.""" kraus = cirq.choi_to_kraus(choi) recovered_choi = cirq.kraus_to_choi(kraus) assert np.allclose(recovered_choi, choi)