def swap_rzz(theta: float, q0: cirq.Qid, q1: cirq.Qid) -> cirq.OP_TREE: """ An implementation of SWAP * EXP(1j theta ZZ) using three sycamore gates. This builds off of the zztheta method. A sycamore gate following the zz-gate is a SWAP EXP(1j (THETA - pi/24) ZZ). Args: theta: exp(1j * theta ) q0: First qubit id to operate on q1: Second qubit id to operate on Returns: The circuit that implements ZZ followed by a swap """ # Set interaction part. circuit = cirq.Circuit() angle_offset = np.pi / 24 - np.pi / 4 circuit.append(google.SYC(q0, q1)) circuit.append(rzz(theta - angle_offset, q0, q1)) # Get the intended circuit. intended_circuit = cirq.Circuit( cirq.SWAP(q0, q1), cirq.ZZPowGate(exponent=2 * theta / np.pi, global_shift=-0.5).on(q0, q1)) yield create_corrected_circuit(intended_circuit, circuit, q0, q1)
def test_get_moment_class(): q0, q1 = cirq.LineQubit.range(2) # Non-hardware assert get_moment_class(cirq.Moment([cirq.CNOT(q0, q1)])) is None # Non-homogenous assert get_moment_class(cirq.Moment([cirq.X(q0), cirq.Z(q1)])) is None # Both phasedxpow assert get_moment_class( cirq.Moment([ cirq.PhasedXPowGate(phase_exponent=0).on(q0), cirq.PhasedXPowGate(phase_exponent=0.5).on(q1) ])) == cirq.PhasedXPowGate # Both Z assert get_moment_class( cirq.Moment([ cirq.ZPowGate(exponent=0.123).on(q0), cirq.ZPowGate(exponent=0.5).on(q1) ])) == cirq.ZPowGate # SYC assert get_moment_class(cirq.Moment([cg.SYC(q0, q1)])) == cg.SycamoreGate # Measurement assert get_moment_class(cirq.Moment([cirq.measure(q0, q1) ])) == cirq.MeasurementGate # Permutation assert get_moment_class( cirq.Moment([QuirkQubitPermutationGate('', '', [1, 0]).on(q0, q1) ])) == QuirkQubitPermutationGate
def test_syc_circuit_diagram(): a, b = cirq.LineQubit.range(2) circuit = cirq.Circuit(cg.SYC(a, b)) cirq.testing.assert_has_diagram( circuit, """ 0: ───SYC─── │ 1: ───SYC─── """, )
def test_validate_well_structured(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit([ cirq.Moment([cirq.PhasedXPowGate(phase_exponent=0).on(q0)]), cirq.Moment([ cirq.ZPowGate(exponent=0.5).on(q0), ]), cirq.Moment([cg.SYC(q0, q1)]), cirq.measure(q0, q1, key='z'), ]) validate_well_structured(circuit)
def test_validate_well_structured_non_term_meas(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit([ cirq.Moment([cirq.PhasedXPowGate(phase_exponent=0).on(q0)]), cirq.Moment([ cirq.PhasedXPowGate(phase_exponent=0.5).on(q0), ]), cirq.measure(q0, q1, key='z'), cirq.Moment([cg.SYC(q0, q1)]), ]) with pytest.raises(BadlyStructuredCircuitError) as e: validate_well_structured(circuit) assert e.match('Measurements must be terminal')
def test_validate_well_structured_too_many(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit([ cirq.Moment([cirq.PhasedXPowGate(phase_exponent=0).on(q0)]), cirq.Moment([ cirq.PhasedXPowGate(phase_exponent=0.5).on(q0), ]), cirq.Moment([cg.SYC(q0, q1)]), cirq.measure(q0, q1, key='z'), ]) with pytest.raises(BadlyStructuredCircuitError) as e: validate_well_structured(circuit) assert e.match('Too many PhX')
def test_find_circuit_structure_violations(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit([ cirq.Moment([cirq.PhasedXPowGate(phase_exponent=0).on(q0)]), cirq.Moment([ cirq.PhasedXPowGate(phase_exponent=0.5).on(q0), cirq.ZPowGate(exponent=1).on(q1) ]), cirq.Moment([cg.SYC(q0, q1)]), cirq.measure(q0, q1, key='z'), cirq.CNOT(q0, q1) ]) violations = find_circuit_structure_violations(circuit) np.testing.assert_array_equal(violations, [1, 4]) assert violations.tolist() == [1, 4]
def test_serialize_deserialize_syc(): proto = op_proto({ 'gate': { 'id': 'syc' }, 'args': {}, 'qubits': [{ 'id': '1_2' }, { 'id': '1_3' }] }) q0 = cirq.GridQubit(1, 2) q1 = cirq.GridQubit(1, 3) op = cg.SYC(q0, q1) assert cg.SYC_GATESET.serialize_op(op) == proto assert cg.SYC_GATESET.deserialize_op(proto) == op
def test_serialize_deserialize_syc(): with cirq.testing.assert_deprecated('SerializableGateSet', deadline='v0.16', count=2): proto = op_proto({ 'gate': { 'id': 'syc' }, 'args': {}, 'qubits': [{ 'id': '1_2' }, { 'id': '1_3' }] }) q0 = cirq.GridQubit(1, 2) q1 = cirq.GridQubit(1, 3) op = cg.SYC(q0, q1) assert cg.SYC_GATESET.serialize_op(op) == proto assert cg.SYC_GATESET.deserialize_op(proto) == op
], ) # 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) @pytest.mark.parametrize( 'op', [ cirq.ISWAP(*cirq.LineQubit.range(2)) ** 0.6, cirq.CZ(*cirq.LineQubit.range(2)) ** 0.3, cirq_google.SYC(*cirq.LineQubit.range(2)), ], ) 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]