Example #1
0
def test_run_batch():
    program = ParentProgram(
        [
            cirq.Circuit(
                cirq.X(Q)**sympy.Symbol('t'), cirq.measure(Q, key='m')),
            cirq.Circuit(
                cirq.X(Q)**sympy.Symbol('x'), cirq.measure(Q, key='m2')),
        ],
        None,
    )
    job = SimulatedLocalJob(
        job_id='test_job',
        processor_id='test1',
        parent_program=program,
        repetitions=100,
        sweeps=[
            cirq.Points(key='t', points=[1, 0]),
            cirq.Points(key='x', points=[0, 1])
        ],
    )
    assert job.execution_status() == quantum.enums.ExecutionStatus.State.READY
    results = job.batched_results()
    assert np.all(results[0][0].measurements['m'] == 1)
    assert np.all(results[0][1].measurements['m'] == 0)
    assert np.all(results[1][0].measurements['m2'] == 0)
    assert np.all(results[1][1].measurements['m2'] == 1)
    assert job.execution_status(
    ) == quantum.enums.ExecutionStatus.State.SUCCESS
Example #2
0
def test_run():
    program = ParentProgram(
        [cirq.Circuit(cirq.X(Q), cirq.measure(Q, key='m'))], None)
    job = SimulatedLocalJob(job_id='test_job',
                            processor_id='test1',
                            parent_program=program,
                            repetitions=100,
                            sweeps=[])
    assert job.id() == 'test_job'
    assert job.execution_status() == quantum.enums.ExecutionStatus.State.READY
    results = job.results()
    assert np.all(results[0].measurements['m'] == 1)
    assert job.execution_status(
    ) == quantum.enums.ExecutionStatus.State.SUCCESS
Example #3
0
def test_run_async():
    qubits = cirq.LineQubit.range(20)
    c = cirq.testing.random_circuit(qubits, n_moments=20, op_density=1.0)
    c.append(cirq.measure(*qubits))
    program = ParentProgram([c], None)
    job = SimulatedLocalJob(
        job_id='test_job',
        processor_id='test1',
        parent_program=program,
        repetitions=100,
        sweeps=[{}],
        simulation_type=LocalSimulationType.ASYNCHRONOUS,
    )
    assert job.execution_status() == quantum.ExecutionStatus.State.RUNNING
    _ = job.results()
    assert job.execution_status() == quantum.ExecutionStatus.State.SUCCESS
Example #4
0
def test_run_batch(simulation_type):
    program = ParentProgram(
        [
            cirq.Circuit(
                cirq.X(Q)**sympy.Symbol('t'), cirq.measure(Q, key='m')),
            cirq.Circuit(
                cirq.X(Q)**sympy.Symbol('x'), cirq.measure(Q, key='m2')),
        ],
        None,
    )
    job = SimulatedLocalJob(
        job_id='test_job',
        processor_id='test1',
        parent_program=program,
        simulation_type=simulation_type,
        repetitions=100,
        sweeps=[
            cirq.Points(key='t', points=[1, 0]),
            cirq.Points(key='x', points=[0, 1])
        ],
    )
    if simulation_type == LocalSimulationType.ASYNCHRONOUS:
        # Note: The simulation could have finished already
        assert (job.execution_status() == quantum.ExecutionStatus.State.RUNNING
                or job.execution_status()
                == quantum.ExecutionStatus.State.SUCCESS)
    else:
        assert job.execution_status() == quantum.ExecutionStatus.State.READY
    results = job.batched_results()
    assert np.all(results[0][0].measurements['m'] == 1)
    assert np.all(results[0][1].measurements['m'] == 0)
    assert np.all(results[1][0].measurements['m2'] == 0)
    assert np.all(results[1][1].measurements['m2'] == 1)
    assert job.execution_status() == quantum.ExecutionStatus.State.SUCCESS
    # Using flattened results
    results = job.results()
    assert np.all(results[0].measurements['m'] == 1)
    assert np.all(results[1].measurements['m'] == 0)
    assert np.all(results[2].measurements['m2'] == 0)
    assert np.all(results[3].measurements['m2'] == 1)

    for result in results:
        assert result.job_id == 'test_job'
        assert result.job_finished_time is not None
    assert results == cirq.read_json(json_text=cirq.to_json(results))
Example #5
0
def test_cancel():
    program = ParentProgram(
        [cirq.Circuit(cirq.X(Q), cirq.measure(Q, key='m'))], None)
    job = SimulatedLocalJob(job_id='test_job',
                            processor_id='test1',
                            parent_program=program,
                            repetitions=100,
                            sweeps=[])
    job.cancel()
    assert job.execution_status() == quantum.ExecutionStatus.State.CANCELLED