Beispiel #1
0
def test_density_matrix_trial_result_qid_shape():
    q0, q1 = cirq.LineQubit.range(2)
    assert (
        cirq.qid_shape(
            cirq.DensityMatrixTrialResult(
                params=cirq.ParamResolver({}),
                measurements={},
                final_simulator_state=cirq.DensityMatrixSimulatorState(
                    density_matrix=np.ones((4, 4)) / 4, qubit_map={q0: 0, q1: 1}
                ),
            ),
        )
        == (2, 2)
    )
    q0, q1 = cirq.LineQid.for_qid_shape((3, 4))
    assert (
        cirq.qid_shape(
            cirq.DensityMatrixTrialResult(
                params=cirq.ParamResolver({}),
                measurements={},
                final_simulator_state=cirq.DensityMatrixSimulatorState(
                    density_matrix=np.ones((12, 12)) / 12, qubit_map={q0: 0, q1: 1}
                ),
            ),
        )
        == (3, 4)
    )
Beispiel #2
0
def test_density_matrix_simulator_state_qid_shape():
    q0, q1 = cirq.LineQubit.range(2)
    assert cirq.qid_shape(
        cirq.DensityMatrixSimulatorState(
            density_matrix=np.ones((4, 4)) / 4, qubit_map={q0: 0, q1: 1}
        )
    ) == (2, 2)
    q0, q1 = cirq.LineQid.for_qid_shape((3, 4))
    assert cirq.qid_shape(
        cirq.DensityMatrixSimulatorState(
            density_matrix=np.ones((12, 12)) / 12, qubit_map={q0: 0, q1: 1}
        )
    ) == (3, 4)
Beispiel #3
0
def test_density_matrix_simulator_state_eq():
    q0, q1 = cirq.LineQubit.range(2)
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(
        cirq.DensityMatrixSimulatorState(density_matrix=np.ones((2, 2)) * 0.5, qubit_map={q0: 0}),
        cirq.DensityMatrixSimulatorState(density_matrix=np.ones((2, 2)) * 0.5, qubit_map={q0: 0}),
    )
    eq.add_equality_group(
        cirq.DensityMatrixSimulatorState(density_matrix=np.eye(2) * 0.5, qubit_map={q0: 0})
    )
    eq.add_equality_group(
        cirq.DensityMatrixSimulatorState(density_matrix=np.eye(2) * 0.5, qubit_map={q0: 0, q1: 1})
    )
def test_density_matrix_trial_result_eq():
    q0 = cirq.LineQubit(0)
    final_simulator_state = cirq.DensityMatrixSimulatorState(
        density_matrix=np.ones((2, 2)) * 0.5,
        qubit_map={q0: 0})
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(
        cirq.DensityMatrixTrialResult(
            params=cirq.ParamResolver({}),
            measurements={},
            final_simulator_state=final_simulator_state),
        cirq.DensityMatrixTrialResult(
            params=cirq.ParamResolver({}),
            measurements={},
            final_simulator_state=final_simulator_state))
    eq.add_equality_group(
        cirq.DensityMatrixTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={},
            final_simulator_state=final_simulator_state))
    eq.add_equality_group(
        cirq.DensityMatrixTrialResult(
            params=cirq.ParamResolver({'s': 1}),
            measurements={'m': np.array([[1]])},
            final_simulator_state=final_simulator_state))
def test_density_matrix_simulator_state_repr():
    q0 = cirq.LineQubit(0)
    assert (repr(cirq.DensityMatrixSimulatorState(
        density_matrix=np.ones((2, 2)) * 0.5, qubit_map={q0: 0}))
            == "cirq.DensityMatrixSimulatorState(density_matrix="
               "np.array([[0.5, 0.5], [0.5, 0.5]]), "
               "qubit_map={cirq.LineQubit(0): 0})")
def test_simulate_moment_steps_empty_circuit(dtype):
    circuit = cirq.Circuit()
    simulator = cirq.DensityMatrixSimulator(dtype=dtype)
    step = None
    for step in simulator.simulate_moment_steps(circuit):
        pass
    assert step.simulator_state() == cirq.DensityMatrixSimulatorState(
        density_matrix=np.array([[1]]), qubit_map={})
def test_density_matrix_trial_result_str():
    q0 = cirq.LineQubit(0)
    final_simulator_state = cirq.DensityMatrixSimulatorState(
        density_matrix=np.ones((2, 2)) * 0.5, qubit_map={q0: 0})
    result = cirq.DensityMatrixTrialResult(
        params=cirq.ParamResolver({}),
        measurements={},
        final_simulator_state=final_simulator_state)

    assert str(result) == ('measurements: (no measurements)\n'
                           'final density matrix:\n'
                           '[[0.5 0.5]\n [0.5 0.5]]')
Beispiel #8
0
def test_density_matrix_trial_result_str():
    q0 = cirq.LineQubit(0)
    final_simulator_state = cirq.DensityMatrixSimulatorState(
        density_matrix=np.ones((2, 2)) * 0.5, qubit_map={q0: 0})
    result = cirq.DensityMatrixTrialResult(
        params=cirq.ParamResolver({}),
        measurements={},
        final_simulator_state=final_simulator_state)

    # numpy varies whitespace in its representation for different versions
    # Eliminate whitespace to harden tests against this variation
    result_no_whitespace = str(result).replace('\n', '').replace(' ', '')
    assert result_no_whitespace == (
        'measurements:(nomeasurements)finaldensitymatrix:[[0.50.5][0.50.5]]')
def test_density_matrix_trial_result_repr():
    q0 = cirq.LineQubit(0)
    final_simulator_state = cirq.DensityMatrixSimulatorState(
        density_matrix=np.ones((2, 2)) * 0.5,
        qubit_map={q0: 0})
    assert (repr(cirq.DensityMatrixTrialResult(
        params=cirq.ParamResolver({'s': 1}),
        measurements={'m': np.array([[1]])},
        final_simulator_state=final_simulator_state)) ==
            "cirq.DensityMatrixTrialResult("
            "params=cirq.ParamResolver({'s': 1}), "
            "measurements={'m': array([[1]])}, "
            "final_simulator_state=cirq.DensityMatrixSimulatorState("
                "density_matrix=np.array([[0.5, 0.5], [0.5, 0.5]]), "
                "qubit_map={cirq.LineQubit(0): 0}))""")