def test_parameterize_phased_fsim_circuit(): 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, _: SQRT_ISWAP(a, b), seed=52 ) p_circuit = parameterize_phased_fsim_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_incremental_simulate(multiprocess): q0, q1 = cirq.LineQubit.range(2) circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=100, two_qubit_op_factory=lambda a, b, _: SQRT_ISWAP(a, b), ) for _ in range(20) ] cycle_depths = np.arange(3, 100, 9) if multiprocess: pool = multiprocessing.Pool() else: pool = None start = time.perf_counter() df_ref = _ref_simulate_2q_xeb_circuits( circuits=circuits, cycle_depths=cycle_depths, pool=pool, ) end1 = time.perf_counter() df = simulate_2q_xeb_circuits(circuits=circuits, cycle_depths=cycle_depths, pool=pool) end2 = time.perf_counter() if pool is not None: pool.terminate() print("\nnew:", end2 - end1, "old:", end1 - start) pd.testing.assert_frame_equal(df_ref, df)
def test_simulate_2q_xeb_circuits(): q0, q1 = cirq.LineQubit.range(2) circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=50, two_qubit_op_factory=lambda a, b, _: SQRT_ISWAP(a, b), ) for _ in range(2) ] cycle_depths = np.arange(3, 50, 9) df = simulate_2q_xeb_circuits( circuits=circuits, cycle_depths=cycle_depths, ) assert len(df) == len(cycle_depths) * len(circuits) for (circuit_i, cycle_depth), row in df.iterrows(): assert 0 <= circuit_i < len(circuits) assert cycle_depth in cycle_depths assert len(row['pure_probs']) == 4 assert np.isclose(np.sum(row['pure_probs']), 1) with multiprocessing.Pool() as pool: df2 = simulate_2q_xeb_circuits(circuits, cycle_depths, pool=pool) pd.testing.assert_frame_equal(df, df2)
def test_simulate_circuit_length_validation(): q0, q1 = cirq.LineQubit.range(2) circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=10, # not long enough! two_qubit_op_factory=lambda a, b, _: SQRT_ISWAP(a, b), ) for _ in range(2) ] cycle_depths = np.arange(3, 50, 9) with pytest.raises(ValueError, match='.*not long enough.*'): _ = simulate_2q_xeb_circuits( circuits=circuits, cycle_depths=cycle_depths, )
def test_benchmark_2q_xeb_fidelities(): q0, q1 = cirq.LineQubit.range(2) circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=50, two_qubit_op_factory=lambda a, b, _: SQRT_ISWAP(a, b), seed=52 ) for _ in range(2) ] cycle_depths = np.arange(3, 50, 9) sampled_df = sample_2q_xeb_circuits( sampler=cirq.Simulator(seed=53), circuits=circuits, cycle_depths=cycle_depths ) fid_df = benchmark_2q_xeb_fidelities(sampled_df, circuits, cycle_depths) assert len(fid_df) == len(cycle_depths) for _, row in fid_df.iterrows(): assert row['cycle_depth'] in cycle_depths assert row['fidelity'] > 0.98
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, _: 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_phased_fsim_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, phased_fsim_options=options, # speed up with looser tolerances: fatol=1e-2, xatol=1e-2, pool=pool, ) assert np.abs(result.x[0] + np.pi / 4) < 0.1 assert np.abs(result.fun) < 0.1 # noiseless simulator
def test_sample_2q_xeb_circuits(): q0, q1 = cirq.LineQubit.range(2) circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=20, two_qubit_op_factory=lambda a, b, _: SQRT_ISWAP(a, b), ) for _ in range(2) ] cycle_depths = np.arange(3, 20, 6) df = sample_2q_xeb_circuits( sampler=cirq.Simulator(), circuits=circuits, cycle_depths=cycle_depths, ) assert len(df) == len(cycle_depths) * len(circuits) for (circuit_i, cycle_depth), row in df.iterrows(): assert 0 <= circuit_i < len(circuits) assert cycle_depth in cycle_depths assert len(row['sampled_probs']) == 4 assert np.isclose(np.sum(row['sampled_probs']), 1)