Ejemplo n.º 1
0
def test_pauli_expansion():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    assert cirq.pauli_expansion(cirq.X(a)) == cirq.LinearDict({'X': 1})
    assert (cirq.pauli_expansion(cirq.CNOT(a, b)) == cirq.pauli_expansion(
        cirq.CNOT))
Ejemplo n.º 2
0
def assert_linear_combinations_of_gates_are_equal(
        actual: cirq.LinearCombinationOfGates,
        expected: cirq.LinearCombinationOfGates) -> None:
    actual_matrix = get_matrix(actual)
    expected_matrix = get_matrix(expected)
    assert np.allclose(actual_matrix, expected_matrix)

    actual_expansion = cirq.pauli_expansion(actual)
    expected_expansion = cirq.pauli_expansion(expected)
    assert set(actual_expansion.keys()) == set(expected_expansion.keys())
    for name in actual_expansion.keys():
        assert abs(actual_expansion[name] - expected_expansion[name]) < 1e-12
Ejemplo n.º 3
0
def test_linear_combination_of_operations_has_correct_pauli_expansion(
        terms, expected_expansion):
    combination = cirq.LinearCombinationOfOperations(terms)
    actual_expansion = cirq.pauli_expansion(combination)
    assert set(actual_expansion.keys()) == set(expected_expansion.keys())
    for name in actual_expansion.keys():
        assert abs(actual_expansion[name] - expected_expansion[name]) < 1e-12
Ejemplo n.º 4
0
def test_pauli_expansion(val, expected_expansion):
    actual_expansion = cirq.pauli_expansion(val)
    assert cirq.approx_eq(actual_expansion, expected_expansion, atol=1e-12)
    assert set(actual_expansion.keys()) == set(expected_expansion.keys())
    for name in actual_expansion.keys():
        assert np.abs(actual_expansion[name] -
                      expected_expansion[name]) < 1e-12
Ejemplo n.º 5
0
def pauli_expansion_for_any_matrix(matrix: np.ndarray) -> cirq.LinearDict[str]:
    class _HasUnitary:
        def __init__(self, matrix: np.ndarray):
            self._matrix = matrix

        def _unitary_(self) -> np.ndarray:
            return self._matrix

    return cirq.pauli_expansion(_HasUnitary(matrix))
Ejemplo n.º 6
0
def assert_linear_combinations_are_equal(
        actual: Union[cirq.LinearCombinationOfGates, cirq.
                      LinearCombinationOfOperations],
        expected: Union[cirq.LinearCombinationOfGates, cirq.
                        LinearCombinationOfOperations]) -> None:
    if not actual and not expected:
        assert len(actual) == 0
        assert len(expected) == 0
        return

    actual_matrix = get_matrix(actual)
    expected_matrix = get_matrix(expected)
    assert np.allclose(actual_matrix, expected_matrix)

    actual_expansion = cirq.pauli_expansion(actual)
    expected_expansion = cirq.pauli_expansion(expected)
    assert set(actual_expansion.keys()) == set(expected_expansion.keys())
    for name in actual_expansion.keys():
        assert abs(actual_expansion[name] - expected_expansion[name]) < 1e-12
Ejemplo n.º 7
0
def test_pauli_expansion():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    assert cirq.pauli_expansion(cirq.X(a)) == cirq.LinearDict({'X': 1})
    assert cirq.pauli_expansion(cirq.CNOT(a, b)) == cirq.pauli_expansion(cirq.CNOT)

    class No(cirq.Gate):
        def num_qubits(self) -> int:
            return 1

    class Yes(cirq.Gate):
        def num_qubits(self) -> int:
            return 1

        def _pauli_expansion_(self):
            return cirq.LinearDict({'X': 0.5})

    assert cirq.pauli_expansion(No().on(a), default=None) is None
    assert cirq.pauli_expansion(Yes().on(a)) == cirq.LinearDict({'X': 0.5})
Ejemplo n.º 8
0
def test_tagged_operation_forwards_protocols():
    """The results of all protocols applied to an operation with a tag should
    be equivalent to the result without tags.
    """
    q1 = cirq.GridQubit(1, 1)
    q2 = cirq.GridQubit(1, 2)
    h = cirq.H(q1)
    tag = 'tag1'
    tagged_h = cirq.H(q1).with_tags(tag)

    np.testing.assert_equal(cirq.unitary(tagged_h), cirq.unitary(h))
    assert cirq.has_unitary(tagged_h)
    assert cirq.decompose(tagged_h) == cirq.decompose(h)
    assert cirq.pauli_expansion(tagged_h) == cirq.pauli_expansion(h)
    assert cirq.equal_up_to_global_phase(h, tagged_h)
    assert np.isclose(cirq.channel(h), cirq.channel(tagged_h)).all()

    assert cirq.measurement_key(cirq.measure(q1, key='blah').with_tags(tag)) == 'blah'

    parameterized_op = cirq.XPowGate(exponent=sympy.Symbol('t'))(q1).with_tags(tag)
    assert cirq.is_parameterized(parameterized_op)
    resolver = cirq.study.ParamResolver({'t': 0.25})
    assert cirq.resolve_parameters(parameterized_op, resolver) == cirq.XPowGate(exponent=0.25)(
        q1
    ).with_tags(tag)
    assert cirq.resolve_parameters_once(parameterized_op, resolver) == cirq.XPowGate(exponent=0.25)(
        q1
    ).with_tags(tag)

    y = cirq.Y(q1)
    tagged_y = cirq.Y(q1).with_tags(tag)
    assert tagged_y ** 0.5 == cirq.YPowGate(exponent=0.5)(q1)
    assert tagged_y * 2 == (y * 2)
    assert 3 * tagged_y == (3 * y)
    assert cirq.phase_by(y, 0.125, 0) == cirq.phase_by(tagged_y, 0.125, 0)
    controlled_y = tagged_y.controlled_by(q2)
    assert controlled_y.qubits == (
        q2,
        q1,
    )
    assert isinstance(controlled_y, cirq.Operation)
    assert not isinstance(controlled_y, cirq.TaggedOperation)

    clifford_x = cirq.SingleQubitCliffordGate.X(q1)
    tagged_x = cirq.SingleQubitCliffordGate.X(q1).with_tags(tag)
    assert cirq.commutes(clifford_x, clifford_x)
    assert cirq.commutes(tagged_x, clifford_x)
    assert cirq.commutes(clifford_x, tagged_x)
    assert cirq.commutes(tagged_x, tagged_x)

    assert cirq.trace_distance_bound(y ** 0.001) == cirq.trace_distance_bound(
        (y ** 0.001).with_tags(tag)
    )

    flip = cirq.bit_flip(0.5)(q1)
    tagged_flip = cirq.bit_flip(0.5)(q1).with_tags(tag)
    assert cirq.has_mixture(tagged_flip)
    assert cirq.has_channel(tagged_flip)

    flip_mixture = cirq.mixture(flip)
    tagged_mixture = cirq.mixture(tagged_flip)
    assert len(tagged_mixture) == 2
    assert len(tagged_mixture[0]) == 2
    assert len(tagged_mixture[1]) == 2
    assert tagged_mixture[0][0] == flip_mixture[0][0]
    assert np.isclose(tagged_mixture[0][1], flip_mixture[0][1]).all()
    assert tagged_mixture[1][0] == flip_mixture[1][0]
    assert np.isclose(tagged_mixture[1][1], flip_mixture[1][1]).all()

    qubit_map = {q1: 'q1'}
    qasm_args = cirq.QasmArgs(qubit_id_map=qubit_map)
    assert cirq.qasm(h, args=qasm_args) == cirq.qasm(tagged_h, args=qasm_args)

    cirq.testing.assert_has_consistent_apply_unitary(tagged_h)
Ejemplo n.º 9
0
def test_raises_no_pauli_expansion(val):
    assert cirq.pauli_expansion(val, default=None) is None
    with pytest.raises(TypeError, match='No Pauli expansion'):
        cirq.pauli_expansion(val)
Ejemplo n.º 10
0
def test_pauli_expansion(terms, expected_expansion):
    combination = cirq.LinearCombinationOfGates(terms)
    actual_expansion = cirq.pauli_expansion(combination)
    assert set(actual_expansion.keys()) == set(expected_expansion.keys())
    for name in actual_expansion.keys():
        assert abs(actual_expansion[name] - expected_expansion[name]) < 1e-12