Ejemplo n.º 1
0
def test_run_executor_identical_circuits_batched(ncircuits, executor):
    collector = Executor(executor=executor, max_batch_size=10)
    circuits = [cirq.Circuit(cirq.H(cirq.LineQubit(0)))] * ncircuits
    results = collector._run(circuits)

    assert np.allclose(results, np.zeros(ncircuits))
    assert collector.calls_to_executor == 1
Ejemplo n.º 2
0
def test_executor_observable_compatibility_check(execute, obs):
    q = cirq.LineQubit(0)
    circuits = [cirq.Circuit(cirq.X(q)), cirq.Circuit(cirq.H(q), cirq.Z(q))]

    executor = Executor(execute)

    with pytest.raises(ValueError, match="are not compatible"):
        executor.evaluate(circuits, obs)
Ejemplo n.º 3
0
def test_run_executor_preserves_order(s, b):
    rng = np.random.RandomState(1)

    collector = Executor(executor=executor_batched_unique, max_batch_size=b)
    assert collector.can_batch

    circuits = [
        cirq.Circuit(cirq.H(cirq.LineQubit(0))),
        cirq.Circuit([cirq.H(cirq.LineQubit(0))] * 2),
    ]
    batch = [circuits[i] for i in rng.random_integers(low=0, high=1, size=s)]

    assert np.allclose(collector._run(batch), executor_batched_unique(batch))
Ejemplo n.º 4
0
def test_run_executor_force_run_all_serial_executor_identical_circuits(
        ncircuits, force_run_all):
    collector = Executor(executor=executor_serial)
    assert not collector.can_batch

    circuits = [cirq.Circuit(cirq.H(cirq.LineQubit(0)))] * ncircuits
    results = collector._run(circuits, force_run_all=force_run_all)

    assert np.allclose(results, np.zeros(ncircuits))
    if force_run_all:
        assert collector.calls_to_executor == ncircuits
    else:
        assert collector.calls_to_executor == 1
Ejemplo n.º 5
0
def test_executor_evaluate_float(execute):
    q = cirq.LineQubit(0)
    circuits = [cirq.Circuit(cirq.X(q)), cirq.Circuit(cirq.H(q), cirq.Z(q))]

    executor = Executor(execute)

    results = executor.evaluate(circuits)
    assert np.allclose(results, [1, 2])

    if execute is executor_serial_unique:
        assert executor.calls_to_executor == 2
    else:
        assert executor.calls_to_executor == 1

    assert executor.executed_circuits == circuits
    assert executor.quantum_results == [1, 2]
Ejemplo n.º 6
0
def test_run_executor_all_unique(ncircuits, batch_size):
    collector = Executor(executor=executor_batched, max_batch_size=batch_size)
    assert collector.can_batch

    random_state = np.random.RandomState(seed=1)
    circuits = [
        cirq.testing.random_circuit(qubits=4,
                                    n_moments=10,
                                    op_density=1,
                                    random_state=random_state)
        for _ in range(ncircuits)
    ]
    results = collector._run(circuits)

    assert np.allclose(results, np.zeros(ncircuits))
    assert collector.calls_to_executor == np.ceil(ncircuits / batch_size)
Ejemplo n.º 7
0
def test_run_executor_nonidentical_pyquil_programs(batch_size):
    collector = Executor(executor=executor_pyquil_batched,
                         max_batch_size=batch_size)
    assert collector.can_batch

    circuits = [
        pyquil.Program(pyquil.gates.X(0)),
        pyquil.Program(pyquil.gates.H(0)),
    ] * 10
    results = collector._run(circuits)

    assert np.allclose(results, np.zeros(len(circuits)))
    if batch_size == 1:
        assert collector.calls_to_executor == 2
    else:
        assert collector.calls_to_executor == 1
Ejemplo n.º 8
0
def test_executor_evaluate_density_matrix(execute):
    obs = Observable(PauliString("Z"))

    q = cirq.LineQubit(0)
    circuits = [cirq.Circuit(cirq.I.on(q)), cirq.Circuit(cirq.X.on(q))]

    executor = Executor(execute)

    results = executor.evaluate(circuits, obs)
    assert np.allclose(results, [1, -1])

    if execute is executor_density_matrix:
        assert executor.calls_to_executor == 2
    else:
        assert executor.calls_to_executor == 1

    assert executor.executed_circuits == circuits
    assert np.allclose(executor.quantum_results[0],
                       executor_density_matrix(circuits[0]))
    assert np.allclose(executor.quantum_results[1],
                       executor_density_matrix(circuits[1]))
    assert len(executor.quantum_results) == len(circuits)
Ejemplo n.º 9
0
def test_executor_evaluate_measurements(execute):
    obs = Observable(PauliString("Z"))

    q = cirq.LineQubit(0)
    circuits = [cirq.Circuit(cirq.I.on(q)), cirq.Circuit(cirq.X.on(q))]

    executor = Executor(execute)

    results = executor.evaluate(circuits, obs)
    assert np.allclose(results, [1, -1])

    if execute is executor_measurements:
        assert executor.calls_to_executor == 2
    else:
        assert executor.calls_to_executor == 1

    assert executor.executed_circuits[0] == circuits[0] + cirq.measure(q)
    assert executor.executed_circuits[1] == circuits[1] + cirq.measure(q)
    assert executor.quantum_results[0] == executor_measurements(
        circuits[0] + cirq.measure(q))
    assert executor.quantum_results[1] == executor_measurements(
        circuits[1] + cirq.measure(q))
    assert len(executor.quantum_results) == len(circuits)
Ejemplo n.º 10
0
def test_executor_is_batched_executor():
    assert Executor.is_batched_executor(executor_batched)
    assert not Executor.is_batched_executor(executor_serial_typed)
    assert not Executor.is_batched_executor(executor_serial)
    assert not Executor.is_batched_executor(executor_measurements)
    assert Executor.is_batched_executor(executor_measurements_batched)
Ejemplo n.º 11
0
def test_executor_simple():
    collector = Executor(executor=executor_batched, max_batch_size=10)
    assert collector.can_batch
    assert collector._max_batch_size == 10
    assert collector.calls_to_executor == 0