def test_transform_sweep():
    qubit = cirq.LineQubit(0)
    a = sympy.Symbol('a')
    circuit = cirq.Circuit.from_ops(
        cirq.X(qubit)**(a / 4),
        cirq.X(qubit)**(1 + a / 2),
    )
    sweep = cirq.Linspace(a, start=0, stop=3, length=4)

    _, new_sweep = cirq.flatten_with_sweep(circuit, sweep)
    assert isinstance(new_sweep, cirq.Sweep)
    resolvers = list(new_sweep)

    expected_resolvers = [
        cirq.ParamResolver({
            '<a/4>': 0.0,
            '<a/2 + 1>': 1.0,
        }),
        cirq.ParamResolver({
            '<a/4>': 0.25,
            '<a/2 + 1>': 1.5,
        }),
        cirq.ParamResolver({
            '<a/4>': 0.5,
            '<a/2 + 1>': 2,
        }),
        cirq.ParamResolver({
            '<a/4>': 0.75,
            '<a/2 + 1>': 2.5,
        })
    ]
    assert resolvers == expected_resolvers
Exemplo n.º 2
0
def test_sweep_with_flattened_sweep():
    q = cirq.GridQubit(0, 0)
    circuit = cirq.Circuit.from_ops(
        cirq.PhasedXPowGate(exponent=sympy.Symbol('t') / 4 + 0.5,
                            phase_exponent=sympy.Symbol('t') / 2 + 0.1,
                            global_shift=0.0)(q), cirq.measure(q, key='m'))
    param_sweep1 = cirq.Linspace('t', start=0, stop=1, length=20)
    (_, param_sweep2) = cirq.flatten_with_sweep(circuit, param_sweep1)
    assert v2.sweep_to_proto(param_sweep2) is not None
Exemplo n.º 3
0
def run_readout_scan(task: ReadoutScanTask, base_dir=None):
    """Execute a :py:class:`ReadoutScanTask` task."""
    if base_dir is None:
        base_dir = DEFAULT_BASE_DIR

    if recirq.exists(task, base_dir=base_dir):
        print(f"{task} already exists. Skipping.")
        return

    # Create a simple circuit
    theta = sympy.Symbol('theta')
    circuit = cirq.Circuit(
        [cirq.ry(theta).on(task.qubit),
         cirq.measure(task.qubit, key='z')])

    # Use utilities to map sampler names to Sampler objects
    sampler = recirq.get_sampler_by_name(device_name=task.device_name)

    # Use a sweep over theta values.
    # Set up limits so we include (-1/2, 0, 1/2, 1, 3/2) * pi
    # The total number of points is resolution_factor * 4 + 1
    n_special_points: int = 5
    resolution_factor = task.resolution_factor
    theta_sweep = cirq.Linspace(theta, -np.pi / 2, 3 * np.pi / 2,
                                resolution_factor * (n_special_points - 1) + 1)
    thetas = np.asarray([v for ((k, v), ) in theta_sweep.param_tuples()])
    flat_circuit, flat_sweep = cirq.flatten_with_sweep(circuit, theta_sweep)

    # Run the jobs
    print(f"Collecting data for {task.qubit}", flush=True)
    results = sampler.run_sweep(program=flat_circuit,
                                params=flat_sweep,
                                repetitions=task.n_shots)

    # Save the results
    recirq.save(task=task,
                data={
                    'thetas':
                    thetas,
                    'all_bitstrings': [
                        recirq.BitArray(np.asarray(r.measurements['z']))
                        for r in results
                    ]
                },
                base_dir=base_dir)