def test_bell_circuit_through_sampler(bell_circuit: cirq.Circuit) -> None:
    """test that RigettiQCSSampler can run a basic bell circuit on the QVM and return an accurate
    ``cirq.study.Result``.
    """
    qc = get_qc('9q-square', as_qvm=True)
    sampler = RigettiQCSSampler(quantum_computer=qc)

    # set the seed so we get a deterministic set of results.
    qvm = cast(QVM, qc.qam)
    qvm.random_seed = 0

    repetitions = 10
    results = sampler.run_sweep(program=bell_circuit,
                                params=[cirq.ParamResolver({})],
                                repetitions=repetitions)
    assert 1 == len(results)

    result = results[0]
    assert isinstance(result, cirq.study.Result)
    assert 0 == len(result.params.param_dict)

    assert 'm' in result.measurements
    assert (repetitions, 2) == result.measurements['m'].shape

    counter = result.histogram(key='m')
    assert 6 == counter.get(0)
    assert counter.get(1) is None
    assert counter.get(2) is None
    assert 4 == counter.get(3)
示例#2
0
def test_parametric_circuit_through_sampler_with_parametric_compilation(
    parametric_circuit_with_params: Tuple[cirq.Circuit, cirq.Linspace]
) -> None:
    """test that RigettiQCSSampler can run a basic parametric circuit on the QVM using parametric
    compilation and return an accurate list of `cirq.study.Result`.
    """
    circuit, sweepable = parametric_circuit_with_params

    qc = get_qc('9q-square', as_qvm=True)
    sampler = RigettiQCSSampler(
        quantum_computer=qc, executor=circuit_sweep_executors.with_quilc_parametric_compilation
    )

    # set the seed so we get a deterministic set of results.
    qvm = cast(QVM, qc.qam)
    qvm.random_seed = 10

    repetitions = 10
    results = sampler.run_sweep(program=circuit, params=sweepable, repetitions=repetitions)
    assert len(sweepable) == len(results)

    expected_results = [(10, 0), (8, 2), (0, 10), (8, 2), (10, 0)]
    for i, result in enumerate(results):
        assert isinstance(result, cirq.study.Result)
        assert sweepable[i] == result.params

        assert 'm' in result.measurements
        assert (repetitions, 1) == result.measurements['m'].shape

        counter = result.histogram(key='m')
        assert expected_results[i][0] == counter[0]
        assert expected_results[i][1] == counter[1]
def test_circuit_with_separate_readout_keys_through_sampler(
    circuit_with_separate_readout_keys: Tuple[cirq.Circuit, cirq.Linspace]
) -> None:
    """test that RigettiQCSSampler can properly readout from separate memory
    regions.
    """
    qc = get_qc('9q-square', as_qvm=True)
    sampler = RigettiQCSSampler(quantum_computer=qc)

    # set the seed so we get a deterministic set of results.
    qvm = cast(QVM, qc.qam)
    qvm.random_seed = 11

    repetitions = 10
    circuit, sweepable = circuit_with_separate_readout_keys
    results = sampler.run_sweep(program=circuit,
                                params=sweepable,
                                repetitions=repetitions)
    assert len(list(sweepable)) == len(results)

    for i, result in enumerate(results):
        assert isinstance(result, cirq.study.Result)
        assert sweepable[i] == result.params

        assert 'm0' in result.measurements
        assert 'm1' in result.measurements
        assert (repetitions, 1) == result.measurements['m0'].shape
        assert (repetitions, 1) == result.measurements['m1'].shape

        counter0 = result.histogram(key='m0')
        counter1 = result.histogram(key='m1')

        assert 2 == len(counter0)
        assert 3 == counter0.get(0)
        assert 7 == counter0.get(1)

        assert 1 == len(counter1)
        assert 10 == counter1.get(0)
        assert counter1.get(1) is None
def test_readout_on_reassigned_qubits(
    circuit_data: Tuple[cirq.Circuit, List[cirq.LineQubit], cirq.Linspace]
) -> None:
    """test that RigettiQCSSampler can properly readout qubits after quilc has
    reassigned those qubits in the compiled native Quil.
    """
    qc = get_qc('9q-square', as_qvm=True)
    circuit, qubits, sweepable = circuit_data

    transformer = circuit_transformers.build(qubit_id_map={
        qubits[0]: '100',
        qubits[1]: '101',
    })
    sampler = RigettiQCSSampler(quantum_computer=qc, transformer=transformer)

    # set the seed so we get a deterministic set of results.
    qvm = cast(QVM, qc.qam)
    qvm.random_seed = 11

    repetitions = 10

    results = sampler.run_sweep(program=circuit,
                                params=sweepable,
                                repetitions=repetitions)
    assert len(sweepable) == len(results)

    for i, result in enumerate(results):
        assert isinstance(result, cirq.study.Result)
        assert sweepable[i] == result.params

        assert 'm' in result.measurements
        assert (repetitions, 2) == result.measurements['m'].shape

        counter = result.histogram(key='m')
        assert 2 == len(counter)
        assert 3 == counter.get(0)
        assert 7 == counter.get(2)