Exemplo n.º 1
0
def test_simulation_trial_result_str():
    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={},
            final_simulator_state=(0, 1))) == '(no measurements)')

    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_simulator_state=(0, 1),
        )) == 'm=1')

    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1, 2, 3])},
            final_simulator_state=(0, 1),
        )) == 'm=123')

    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([9, 10, 11])},
            final_simulator_state=(0, 1),
        )) == 'm=9 10 11')
Exemplo n.º 2
0
def test_intermediate_sweeps():
    simulator = cirq.SimulatesIntermediateState()

    final_state = np.array([1, 0, 0, 0])
    def steps(*args, **kwargs):
        result = mock.Mock()
        result.measurements = {'a': np.array([True, True])}
        result._simulator_state.return_value = final_state
        yield result

    simulator._simulator_iterator.side_effect = steps
    circuit = mock.Mock(cirq.Circuit)
    param_resolvers = [mock.Mock(cirq.ParamResolver),
                       mock.Mock(cirq.ParamResolver)]
    qubit_order = mock.Mock(cirq.QubitOrder)
    results = simulator.simulate_sweep(program=circuit,
                                       params=param_resolvers,
                                       qubit_order=qubit_order,
                                       initial_state=2)
    expected_results = [
        cirq.SimulationTrialResult(
            measurements={'a': np.array([True, True])},
            params=param_resolvers[0],
            final_simulator_state=final_state),
        cirq.SimulationTrialResult(
            measurements={'a': np.array([True, True])},
            params=param_resolvers[1],
            final_simulator_state=final_state)
    ]
    assert results == expected_results
Exemplo n.º 3
0
def test_simulation_trial_result_equality():
    eq = cirq.testing.EqualsTester()
    final_step_result = mock.Mock(cirq.StepResult)
    final_step_result._simulator_state.return_value = ()
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({}),
                                   measurements={},
                                   final_step_result=final_step_result),
        cirq.SimulationTrialResult(params=cirq.ParamResolver({}),
                                   measurements={},
                                   final_step_result=final_step_result),
    )
    eq.add_equality_group(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={},
            final_step_result=final_step_result,
        ))
    eq.add_equality_group(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_step_result=final_step_result,
        ))
    final_step_result._simulator_state.return_value = (0, 1)
    eq.add_equality_group(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_step_result=final_step_result,
        ))
Exemplo n.º 4
0
def test_simulation_trial_result_equality():
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({}),
                                   measurements={},
                                   final_simulator_state=()),
        cirq.SimulationTrialResult(params=cirq.ParamResolver({}),
                                   measurements={},
                                   final_simulator_state=()),
    )
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({'s': 1}),
                                   measurements={},
                                   final_simulator_state=()))
    eq.add_equality_group(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_simulator_state=(),
        ))
    eq.add_equality_group(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_simulator_state=(0, 1),
        ))
Exemplo n.º 5
0
def test_simulation_trial_result_str():
    final_step_result = mock.Mock(cirq.StepResult)
    final_step_result._simulator_state.return_value = (0, 1)
    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={},
            final_step_result=final_step_result,
        )) == '(no measurements)')

    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_step_result=final_step_result,
        )) == 'm=1')

    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1, 2, 3])},
            final_step_result=final_step_result,
        )) == 'm=123')

    assert (str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([9, 10, 11])},
            final_step_result=final_step_result,
        )) == 'm=9 10 11')
Exemplo n.º 6
0
def test_simulation_trial_result_str():
    assert str(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={},
            final_simulator_state=(0, 1))) == '(no measurements)'

    assert str(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({'s': 1}),
                                   measurements={'m': np.array([[1]])},
                                   final_simulator_state=(0, 1))) == 'm=1'
Exemplo n.º 7
0
def test_simulation_trial_result_repr():
    assert repr(cirq.SimulationTrialResult(params=cirq.ParamResolver({'s': 1}),
                                           measurements={'m': np.array([[1]])},
                                           final_simulator_state=(0, 1))) == (
               "cirq.SimulationTrialResult("
               "params=cirq.ParamResolver({'s': 1}), "
               "measurements={'m': array([[1]])}, "
               "final_simulator_state=(0, 1))")
Exemplo n.º 8
0
def test_simulator_trial_result_equality():
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 2}),
                                   measurements={'m': np.array([1, 0])},
                                   final_state=np.array([0, 1, 0, 0])))
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 2}),
                                   measurements={'m': np.array([1, 0])},
                                   final_state=np.array([0, 0, 1, 0])))
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 3}),
                                   measurements={'m': np.array([1, 0])},
                                   final_state=np.array([0, 0, 1, 0])))
    eq.add_equality_group(
        cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 3}),
                                   measurements={'m': np.array([0, 1])},
                                   final_state=np.array([0, 0, 1, 0])))
Exemplo n.º 9
0
def test_simulator_simulate_trial_result_repr():
    v = cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 2}),
                                   measurements={'m': np.array([1, 2])},
                                   final_state=np.array([0, 1, 0, 0]))

    assert repr(v) == ("SimulationTrialResult("
                       "params=cirq.ParamResolver({'a': 2}), "
                       "measurements={'m': array([1, 2])}, "
                       "final_state=array([0, 1, 0, 0]))")
Exemplo n.º 10
0
def test_deprecated_setters():
    step = FakeStepResult()
    result = cirq.SimulationTrialResult(cirq.ParamResolver(), {}, 0)
    with cirq.testing.assert_deprecated(deadline='v0.16'):
        step.measurements = {}
    with cirq.testing.assert_deprecated(deadline='v0.16'):
        result.measurements = {}
    with cirq.testing.assert_deprecated(deadline='v0.16'):
        result.params = cirq.ParamResolver()
Exemplo n.º 11
0
def test_simulation_trial_result_repr():
    final_step_result = mock.Mock(cirq.StepResult)
    final_step_result._simulator_state.return_value = (0, 1)
    assert repr(
        cirq.SimulationTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([1])},
            final_step_result=final_step_result,
        )) == ("cirq.SimulationTrialResult("
               "params=cirq.ParamResolver({'s': 1}), "
               "measurements={'m': array([1])}, "
               "final_simulator_state=(0, 1))")
Exemplo n.º 12
0
def test_pretty_print():
    result = cirq.SimulationTrialResult(cirq.ParamResolver(), {}, np.array([1]))

    # Test Jupyter console output from
    class FakePrinter:
        def __init__(self):
            self.text_pretty = ''

        def text(self, to_print):
            self.text_pretty += to_print

    p = FakePrinter()
    result._repr_pretty_(p, False)
    assert p.text_pretty == '(no measurements)'

    # Test cycle handling
    p = FakePrinter()
    result._repr_pretty_(p, True)
    assert p.text_pretty == 'SimulationTrialResult(...)'
Exemplo n.º 13
0
def test_simulator_trial_pretty_state():
    result = cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 2}),
                                        measurements={'m': np.array([1, 2])},
                                        final_state=np.array([0, 1, 0, 0]))
    assert result.dirac_notation() == '|01⟩'
Exemplo n.º 14
0
def test_simulator_trial_bloch_vector():
    result = cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 2}),
                                        measurements={'m': np.array([1, 2])},
                                        final_state=np.array([0, 1, 0, 0]))
    bloch = np.array([0, 0, -1])
    np.testing.assert_array_almost_equal(bloch, result.bloch_vector(1))
Exemplo n.º 15
0
def test_simulator_trial_density_matrix():
    result = cirq.SimulationTrialResult(params=cirq.ParamResolver({'a': 2}),
                                        measurements={'m': np.array([1, 2])},
                                        final_state=np.array([0, 1, 0, 0]))
    rho = np.array([[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
    np.testing.assert_array_almost_equal(rho, result.density_matrix())