def test_result_override(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) options = WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION result = PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization( theta=0.1, zeta=0.2, chi=None, gamma=0.4, phi=0.5 ), (q_02, q_03): PhasedFSimCharacterization( theta=0.6, zeta=0.7, chi=None, gamma=0.9, phi=1.0 ), }, gate=gate, options=options, ) overridden = result.override(options.zeta_chi_gamma_correction_override()) assert overridden == PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization( theta=0.1, zeta=0.0, chi=None, gamma=0.0, phi=0.5 ), (q_02, q_03): PhasedFSimCharacterization( theta=0.6, zeta=0.0, chi=None, gamma=0.0, phi=1.0 ), }, gate=gate, options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION, )
def test_merge_matching_results(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) options = WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION parameters_1 = { (q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3), } parameters_2 = { (q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6), } results = [ PhasedFSimCalibrationResult( parameters=parameters_1, gate=gate, options=options, ), PhasedFSimCalibrationResult( parameters=parameters_2, gate=gate, options=options, ), ] assert merge_matching_results(results) == PhasedFSimCalibrationResult( parameters={ **parameters_1, **parameters_2 }, gate=gate, options=options, )
def test_get_parameters(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) result = PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3), (q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6), }, gate=gate, options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), ) assert result.get_parameters(q_00, q_01) == PhasedFSimCharacterization( theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3) assert result.get_parameters(q_01, q_00) == PhasedFSimCharacterization( theta=0.1, zeta=-0.2, chi=None, gamma=None, phi=0.3) assert result.get_parameters(q_02, q_03) == PhasedFSimCharacterization( theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6) assert result.get_parameters(q_00, q_03) is None
def get_calibrations( self, requests: Sequence[PhasedFSimCalibrationRequest] ) -> List[PhasedFSimCalibrationResult]: """Retrieves the calibration that matches the requests Args: requests: Calibration requests to obtain. Returns: Calibration results that reflect the internal state of simulator. """ results = [] for request in requests: if isinstance(request, FloquetPhasedFSimCalibrationRequest): options = request.options characterize_theta = options.characterize_theta characterize_zeta = options.characterize_zeta characterize_chi = options.characterize_chi characterize_gamma = options.characterize_gamma characterize_phi = options.characterize_phi else: raise ValueError(f'Unsupported calibration request {request}') translated = self.gates_translator(request.gate) if translated is None: raise ValueError( f'Calibration request contains unsupported gate {request.gate}' ) parameters = {} for a, b in request.pairs: drifted = self.create_gate_with_drift(a, b, translated) parameters[a, b] = PhasedFSimCharacterization( theta=drifted.theta if characterize_theta else None, zeta=drifted.zeta if characterize_zeta else None, chi=drifted.chi if characterize_chi else None, gamma=drifted.gamma if characterize_gamma else None, phi=drifted.phi if characterize_phi else None, ) results.append( PhasedFSimCalibrationResult(parameters=parameters, gate=request.gate, options=options)) return results
def test_run_characterization_with_simulator(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = SQRT_ISWAP_GATE request = FloquetPhasedFSimCalibrationRequest( gate=gate, pairs=((q_00, q_01), (q_02, q_03)), options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), ) simulator = PhasedFSimEngineSimulator.create_with_ideal_sqrt_iswap() actual = workflow.run_characterizations([request], simulator) assert actual == [ PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization(theta=np.pi / 4, zeta=0.0, chi=None, gamma=None, phi=0.0), (q_02, q_03): PhasedFSimCharacterization(theta=np.pi / 4, zeta=0.0, chi=None, gamma=None, phi=0.0), }, gate=SQRT_ISWAP_GATE, options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), ) ]
def test_zeta_chi_gamma_calibration_for_moments(): a, b = cirq.LineQubit.range(2) characterizations = [ PhasedFSimCalibrationResult( parameters={(a, b): SQRT_ISWAP_PARAMETERS}, gate=SQRT_ISWAP_GATE, options=ALL_ANGLES_FLOQUET_PHASED_FSIM_CHARACTERIZATION, ) ] moment_allocations = [0] for circuit in [ cirq.Circuit(cirq.FSimGate(theta=np.pi / 4, phi=0.0).on(a, b)), cirq.Circuit(cirq.FSimGate(theta=-np.pi / 4, phi=0.0).on(a, b)), ]: calibrated_circuit = workflow.make_zeta_chi_gamma_compensation_for_moments( workflow.CircuitWithCalibration(circuit, moment_allocations), characterizations) assert np.allclose(cirq.unitary(circuit), cirq.unitary(calibrated_circuit.circuit)) assert calibrated_circuit.moment_to_calibration == [None, 0, None]
def test_floquet_parse_result(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) request = FloquetPhasedFSimCalibrationRequest( gate=gate, pairs=((q_00, q_01), (q_02, q_03)), options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), ) result = cirq.google.CalibrationResult( code=cirq.google.api.v2.calibration_pb2.SUCCESS, error_message=None, token=None, valid_until=None, metrics=cirq.google.Calibration( cirq.google.api.v2.metrics_pb2.MetricsSnapshot(metrics=[ cirq.google.api.v2.metrics_pb2.Metric( name='angles', targets=[ '0_qubit_a', '0_qubit_b', '0_theta_est', '0_zeta_est', '0_phi_est', '1_qubit_a', '1_qubit_b', '1_theta_est', '1_zeta_est', '1_phi_est', ], values=[ cirq.google.api.v2.metrics_pb2.Value(str_val='0_0'), cirq.google.api.v2.metrics_pb2.Value(str_val='0_1'), cirq.google.api.v2.metrics_pb2.Value(double_val=0.1), cirq.google.api.v2.metrics_pb2.Value(double_val=0.2), cirq.google.api.v2.metrics_pb2.Value(double_val=0.3), cirq.google.api.v2.metrics_pb2.Value(str_val='0_2'), cirq.google.api.v2.metrics_pb2.Value(str_val='0_3'), cirq.google.api.v2.metrics_pb2.Value(double_val=0.4), cirq.google.api.v2.metrics_pb2.Value(double_val=0.5), cirq.google.api.v2.metrics_pb2.Value(double_val=0.6), ], ) ])), ) assert request.parse_result(result) == PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3), (q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6), }, gate=gate, options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), )
def test_merge_matching_results_when_incompatible_fails(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) options = WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION parameters_1 = { (q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3), } parameters_2 = { (q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6), } with pytest.raises(ValueError): results = [ PhasedFSimCalibrationResult( parameters=parameters_1, gate=gate, options=options, ), PhasedFSimCalibrationResult( parameters=parameters_1, gate=gate, options=options, ), ] assert merge_matching_results(results) with pytest.raises(ValueError): results = [ PhasedFSimCalibrationResult( parameters=parameters_1, gate=gate, options=options, ), PhasedFSimCalibrationResult( parameters=parameters_2, gate=cirq.CZ, options=options, ), ] assert merge_matching_results(results) with pytest.raises(ValueError): results = [ PhasedFSimCalibrationResult( parameters=parameters_1, gate=gate, options=options, ), PhasedFSimCalibrationResult( parameters=parameters_2, gate=gate, options=ALL_ANGLES_FLOQUET_PHASED_FSIM_CHARACTERIZATION, ), ] assert merge_matching_results(results)
def test_zeta_chi_gamma_calibration_for_moments_invalid_argument_fails( ) -> None: a, b, c = cirq.LineQubit.range(3) with pytest.raises(ValueError): circuit_with_calibration = workflow.CircuitWithCalibration( cirq.Circuit(), [1]) workflow.zeta_chi_gamma_calibration_for_moments( circuit_with_calibration, []) with pytest.raises(ValueError): circuit_with_calibration = workflow.CircuitWithCalibration( cirq.Circuit(SQRT_ISWAP_GATE.on(a, b)), [None]) workflow.zeta_chi_gamma_calibration_for_moments( circuit_with_calibration, []) with pytest.raises(ValueError): circuit_with_calibration = workflow.CircuitWithCalibration( cirq.Circuit(SQRT_ISWAP_GATE.on(a, b)), [0]) characterizations = [ PhasedFSimCalibrationResult( parameters={}, gate=SQRT_ISWAP_GATE, options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION, ) ] workflow.zeta_chi_gamma_calibration_for_moments( circuit_with_calibration, characterizations) with pytest.raises(workflow.IncompatibleMomentError): circuit_with_calibration = workflow.CircuitWithCalibration( cirq.Circuit(cirq.GlobalPhaseOperation(coefficient=1.0)), [None]) workflow.zeta_chi_gamma_calibration_for_moments( circuit_with_calibration, []) with pytest.raises(workflow.IncompatibleMomentError): circuit_with_calibration = workflow.CircuitWithCalibration( cirq.Circuit(cirq.CZ.on(a, b)), [None]) workflow.zeta_chi_gamma_calibration_for_moments( circuit_with_calibration, []) with pytest.raises(workflow.IncompatibleMomentError): circuit_with_calibration = workflow.CircuitWithCalibration( cirq.Circuit([SQRT_ISWAP_GATE.on(a, b), cirq.Z.on(c)]), [0]) characterizations = [ PhasedFSimCalibrationResult( parameters={ (a, b): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=0.3, gamma=0.4, phi=0.5) }, gate=SQRT_ISWAP_GATE, options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION, ) ] workflow.zeta_chi_gamma_calibration_for_moments( circuit_with_calibration, characterizations)
def test_run_floquet_characterization_for_circuit(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) circuit = cirq.Circuit([gate.on(q_00, q_01), gate.on(q_02, q_03)]) options = FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ) job = cirq.google.engine.EngineJob('', '', '', None) job._calibration_results = [ cirq.google.CalibrationResult( code=cirq.google.api.v2.calibration_pb2.SUCCESS, error_message=None, token=None, valid_until=None, metrics=cirq.google.Calibration( cirq.google.api.v2.metrics_pb2.MetricsSnapshot(metrics=[ cirq.google.api.v2.metrics_pb2.Metric( name='angles', targets=[ '0_qubit_a', '0_qubit_b', '0_theta_est', '0_zeta_est', '0_phi_est', '1_qubit_a', '1_qubit_b', '1_theta_est', '1_zeta_est', '1_phi_est', ], values=[ cirq.google.api.v2.metrics_pb2.Value( str_val='0_0'), cirq.google.api.v2.metrics_pb2.Value( str_val='0_1'), cirq.google.api.v2.metrics_pb2.Value( double_val=0.1), cirq.google.api.v2.metrics_pb2.Value( double_val=0.2), cirq.google.api.v2.metrics_pb2.Value( double_val=0.3), cirq.google.api.v2.metrics_pb2.Value( str_val='0_2'), cirq.google.api.v2.metrics_pb2.Value( str_val='0_3'), cirq.google.api.v2.metrics_pb2.Value( double_val=0.4), cirq.google.api.v2.metrics_pb2.Value( double_val=0.5), cirq.google.api.v2.metrics_pb2.Value( double_val=0.6), ], ) ])), ) ] engine = mock.MagicMock(spec=cirq.google.Engine) engine.run_calibration.return_value = job circuit_with_calibration, requests = workflow.run_floquet_characterization_for_circuit( circuit, engine, 'qproc', cirq.google.FSIM_GATESET, options=options) assert requests == [ PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3), (q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6), }, gate=gate, options=options, ) ] assert circuit_with_calibration.circuit == circuit assert circuit_with_calibration.moment_to_calibration == [0]
def test_run_characterization(): q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)] gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0) request = FloquetPhasedFSimCalibrationRequest( gate=gate, pairs=((q_00, q_01), (q_02, q_03)), options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), ) result = cirq.google.CalibrationResult( code=cirq.google.api.v2.calibration_pb2.SUCCESS, error_message=None, token=None, valid_until=None, metrics=cirq.google.Calibration( cirq.google.api.v2.metrics_pb2.MetricsSnapshot(metrics=[ cirq.google.api.v2.metrics_pb2.Metric( name='angles', targets=[ '0_qubit_a', '0_qubit_b', '0_theta_est', '0_zeta_est', '0_phi_est', '1_qubit_a', '1_qubit_b', '1_theta_est', '1_zeta_est', '1_phi_est', ], values=[ cirq.google.api.v2.metrics_pb2.Value(str_val='0_0'), cirq.google.api.v2.metrics_pb2.Value(str_val='0_1'), cirq.google.api.v2.metrics_pb2.Value(double_val=0.1), cirq.google.api.v2.metrics_pb2.Value(double_val=0.2), cirq.google.api.v2.metrics_pb2.Value(double_val=0.3), cirq.google.api.v2.metrics_pb2.Value(str_val='0_2'), cirq.google.api.v2.metrics_pb2.Value(str_val='0_3'), cirq.google.api.v2.metrics_pb2.Value(double_val=0.4), cirq.google.api.v2.metrics_pb2.Value(double_val=0.5), cirq.google.api.v2.metrics_pb2.Value(double_val=0.6), ], ) ])), ) job = cirq.google.engine.EngineJob('', '', '', None) job._calibration_results = [result] engine = mock.MagicMock(spec=cirq.google.Engine) engine.run_calibration.return_value = job progress_calls = [] def progress(step: int, steps: int) -> None: progress_calls.append((step, steps)) actual = workflow.run_characterizations([request], engine, 'qproc', cirq.google.FSIM_GATESET, progress_func=progress) expected = [ PhasedFSimCalibrationResult( parameters={ (q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3), (q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6), }, gate=gate, options=FloquetPhasedFSimCalibrationOptions( characterize_theta=True, characterize_zeta=True, characterize_chi=False, characterize_gamma=False, characterize_phi=True, ), ) ] assert actual == expected assert progress_calls == [(1, 1)]