def test_random_rotation_between_two_qubit_circuit(): q0, q1 = cirq.LineQubit.range(2) circuit = random_rotations_between_two_qubit_circuit(q0, q1, 4, seed=52) assert len(circuit) == 4 * 2 + 1 assert circuit.all_qubits() == {q0, q1} circuit = random_rotations_between_two_qubit_circuit( q0, q1, 4, seed=52, add_final_single_qubit_layer=False ) assert len(circuit) == 4 * 2 assert circuit.all_qubits() == {q0, q1} cirq.testing.assert_has_diagram( circuit, """\ 0 1 │ │ Y^0.5 X^0.5 │ │ @─────────────@ │ │ PhX(0.25)^0.5 Y^0.5 │ │ @─────────────@ │ │ Y^0.5 X^0.5 │ │ @─────────────@ │ │ X^0.5 PhX(0.25)^0.5 │ │ @─────────────@ │ │""", transpose=True, )
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_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, _: cirq.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_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, _: cirq.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_sample_2q_xeb_circuits(): q0 = cirq.NamedQubit('a') q1 = cirq.NamedQubit('b') circuits = [ rqcg.random_rotations_between_two_qubit_circuit( q0, q1, depth=20, two_qubit_op_factory=lambda a, b, _: cirq.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, shuffle=np.random.RandomState(10), ) 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)
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 fit_df = fit_exponential_decays(fid_df) for _, row in fit_df.iterrows(): assert list(row['cycle_depths']) == list(cycle_depths) assert len(row['fidelities']) == len(cycle_depths)
def circuits_cycle_depths_sampled_df(): 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(10, 40 + 1, 10) sampled_df = sample_2q_xeb_circuits( sampler=cirq.Simulator(seed=53), circuits=circuits, cycle_depths=cycle_depths ) return circuits, cycle_depths, sampled_df
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, _: cirq.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_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)