def test_parameterize_phased_fsim_circuit(gate): q0, q1 = cirq.LineQubit.range(2) circuit = rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=3, two_qubit_op_factory=lambda a, b, _: gate(a, b), seed=52) p_circuit = parameterize_circuit(circuit, SqrtISwapXEBOptions()) cirq.testing.assert_has_diagram( p_circuit, """\ 0 1 │ │ Y^0.5 X^0.5 │ │ PhFSim(theta, zeta, chi, gamma, phi)─PhFSim(theta, zeta, chi, gamma, phi) │ │ PhX(0.25)^0.5 Y^0.5 │ │ PhFSim(theta, zeta, chi, gamma, phi)─PhFSim(theta, zeta, chi, gamma, phi) │ │ Y^0.5 X^0.5 │ │ PhFSim(theta, zeta, chi, gamma, phi)─PhFSim(theta, zeta, chi, gamma, phi) │ │ X^0.5 PhX(0.25)^0.5 │ │ """, transpose=True, )
def test_characterize_phased_fsim_parameters_with_xeb(): q0, q1 = cirq.LineQubit.range(2) rs = np.random.RandomState(52) circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=20, two_qubit_op_factory=lambda a, b, _: cirq.SQRT_ISWAP(a, b), seed=rs, ) for _ in range(2) ] cycle_depths = np.arange(3, 20, 6) sampled_df = sample_2q_xeb_circuits( sampler=cirq.Simulator(seed=rs), circuits=circuits, cycle_depths=cycle_depths, progress_bar=None, ) # only optimize theta so it goes faster. options = SqrtISwapXEBOptions( characterize_theta=True, characterize_gamma=False, characterize_chi=False, characterize_zeta=False, characterize_phi=False, ) p_circuits = [ parameterize_circuit(circuit, options) for circuit in circuits ] with multiprocessing.Pool() as pool: result = characterize_phased_fsim_parameters_with_xeb( sampled_df=sampled_df, parameterized_circuits=p_circuits, cycle_depths=cycle_depths, options=options, # speed up with looser tolerances: fatol=1e-2, xatol=1e-2, pool=pool, ) opt_res = result.optimization_results[(q0, q1)] assert np.abs(opt_res.x[0] + np.pi / 4) < 0.1 assert np.abs(opt_res.fun) < 0.1 # noiseless simulator assert len(result.fidelities_df) == len(cycle_depths) assert np.all(result.fidelities_df['fidelity'] > 0.95)
def test_parallel_full_workflow(use_pool): circuits = rqcg.generate_library_of_2q_circuits( n_library_circuits=5, two_qubit_gate=cirq.ISWAP**0.5, max_cycle_depth=4, random_state=8675309, ) cycle_depths = [2, 3, 4] graph = _gridqubits_to_graph_device(cirq.GridQubit.rect(2, 2)) combs = rqcg.get_random_combinations_for_device( n_library_circuits=len(circuits), n_combinations=2, device_graph=graph, random_state=10) sampled_df = sample_2q_xeb_circuits( sampler=cirq.Simulator(), circuits=circuits, cycle_depths=cycle_depths, combinations_by_layer=combs, ) if use_pool: pool = multiprocessing.Pool() else: pool = None fids_df_0 = benchmark_2q_xeb_fidelities(sampled_df=sampled_df, circuits=circuits, cycle_depths=cycle_depths, pool=pool) options = SqrtISwapXEBOptions(characterize_zeta=False, characterize_gamma=False, characterize_chi=False) p_circuits = [ parameterize_circuit(circuit, options) for circuit in circuits ] result = characterize_phased_fsim_parameters_with_xeb_by_pair( sampled_df=sampled_df, parameterized_circuits=p_circuits, cycle_depths=cycle_depths, options=options, # super loose tolerances fatol=5e-2, xatol=5e-2, pool=pool, ) if pool is not None: pool.terminate() assert len(result.optimization_results) == graph.number_of_edges() for opt_res in result.optimization_results.values(): assert np.abs(opt_res.fun) < 0.1 # noiseless simulator assert len( result.fidelities_df) == len(cycle_depths) * graph.number_of_edges() assert np.all(result.fidelities_df['fidelity'] > 0.90) before_after_df = before_and_after_characterization( fids_df_0, characterization_result=result) for _, row in before_after_df.iterrows(): assert len(row['fidelities_0']) == len(cycle_depths) assert len(row['fidelities_c']) == len(cycle_depths) assert 0 <= row['a_0'] <= 1 assert 0 <= row['a_c'] <= 1 assert 0 <= row['layer_fid_0'] <= 1 assert 0 <= row['layer_fid_c'] <= 1
def test_get_initial_simplex(): options = SqrtISwapXEBOptions() simplex, names = options.get_initial_simplex_and_names() assert names == ['theta', 'zeta', 'chi', 'gamma', 'phi'] assert len(simplex) == len(names) + 1 assert simplex.shape[1] == len(names)