Beispiel #1
0
def test_linear_combinations_of_gates_valid_powers(terms, exponent,
                                                   expected_terms):
    combination = cirq.LinearCombinationOfGates(terms)
    actual_result = combination**exponent
    expected_result = cirq.LinearCombinationOfGates(expected_terms)
    assert cirq.approx_eq(actual_result, expected_result)
    assert len(actual_result) == len(expected_terms)
Beispiel #2
0
def test_linear_combination_of_gates_accepts_consistent_gates(terms):
    combination_1 = cirq.LinearCombinationOfGates(terms)

    combination_2 = cirq.LinearCombinationOfGates({})
    combination_2.update(terms)

    combination_3 = cirq.LinearCombinationOfGates({})
    for gate, coefficient in terms.items():
        combination_3[gate] += coefficient

    assert combination_1 == combination_2 == combination_3
Beispiel #3
0
def test_linear_combination_of_gates_rejects_inconsistent_gates(terms):
    with pytest.raises(ValueError):
        cirq.LinearCombinationOfGates(terms)

    combination = cirq.LinearCombinationOfGates({})
    with pytest.raises(ValueError):
        combination.update(terms)

    combination = cirq.LinearCombinationOfGates({})
    with pytest.raises(ValueError):
        for gate, coefficient in terms.items():
            combination[gate] += coefficient
Beispiel #4
0
def test_in_place_manipulations_of_linear_combination_of_gates(gates):
    a = cirq.LinearCombinationOfGates({})
    b = cirq.LinearCombinationOfGates({})

    for i, gate in enumerate(gates):
        a += gate
        b -= gate

        prefix = gates[:i + 1]
        expected_a = cirq.LinearCombinationOfGates(collections.Counter(prefix))
        expected_b = -expected_a

        assert_linear_combinations_are_equal(a, expected_a)
        assert_linear_combinations_are_equal(b, expected_b)
Beispiel #5
0
def test_linear_combination_of_gates_has_correct_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
def test_parameterized_linear_combination_of_gates(terms, is_parameterized,
                                                   parameter_names):
    gate = cirq.LinearCombinationOfGates(terms)
    assert cirq.is_parameterized(gate) == is_parameterized
    assert cirq.parameter_names(gate) == parameter_names
    resolved = cirq.resolve_parameters(gate, {p: 1 for p in parameter_names})
    assert not cirq.is_parameterized(resolved)
Beispiel #7
0
def test_empty_linear_combination_of_gates_has_no_matrix():
    empty = cirq.LinearCombinationOfGates({})
    assert empty.num_qubits() is None
    with pytest.raises(ValueError):
        empty.matrix()
Beispiel #8
0
def test_linear_combination_of_gates_has_correct_num_qubits(
        terms, expected_num_qubits):
    combination = cirq.LinearCombinationOfGates(terms)
    assert combination.num_qubits() == expected_num_qubits
Beispiel #9
0
def test_empty_linear_combination_of_gates_accepts_all_gates(gate):
    combination = cirq.LinearCombinationOfGates({})
    combination[gate] = -0.5j
    assert len(combination) == 1
Beispiel #10
0
def test_linear_combinations_of_gates_invalid_powers(terms, exponent):
    combination = cirq.LinearCombinationOfGates(terms)
    with pytest.raises(TypeError):
        _ = combination**exponent
Beispiel #11
0
def test_linear_combination_of_gates_has_correct_matrix(terms, expected_matrix):
    combination = cirq.LinearCombinationOfGates(terms)
    assert np.all(combination.matrix() == expected_matrix)
Beispiel #12
0
    (cirq.X - cirq.Y, -cirq.Y + cirq.X),
    (cirq.X + cirq.S - cirq.X, cirq.S),
    (cirq.Y - 2 * cirq.Y, -cirq.Y),
    (cirq.rx(0.2), np.cos(0.1) * cirq.I - 1j * np.sin(0.1) * cirq.X),
    (1j * cirq.H * 1j, -cirq.H),
    (-1j * cirq.Y, cirq.ry(np.pi)),
    (np.sqrt(-1j) * cirq.S, cirq.rz(np.pi / 2)),
    (0.5 * (cirq.IdentityGate(2) + cirq.XX + cirq.YY + cirq.ZZ), cirq.SWAP),
    ((cirq.IdentityGate(2) + 1j *
      (cirq.XX + cirq.YY) + cirq.ZZ) / 2, cirq.ISWAP),
    (cirq.CNOT + 0 * cirq.SWAP, cirq.CNOT),
    (0.5 * cirq.FREDKIN, cirq.FREDKIN / 2),
    (cirq.FREDKIN * 0.5, cirq.FREDKIN / 2),
    (((cirq.X + cirq.Y) / np.sqrt(2))**2, cirq.I),
    ((cirq.X + cirq.Z)**3, 2 * (cirq.X + cirq.Z)),
    ((cirq.X + 1j * cirq.Y)**2, cirq.LinearCombinationOfGates({})),
    ((cirq.X - 1j * cirq.Y)**2, cirq.LinearCombinationOfGates({})),
    (((3 * cirq.X - 4 * cirq.Y + 12 * cirq.Z) / 13)**24, cirq.I),
    (((3 * cirq.X - 4 * cirq.Y + 12 * cirq.Z) / 13)**25,
     (3 * cirq.X - 4 * cirq.Y + 12 * cirq.Z) / 13),
    ((cirq.X + cirq.Y + cirq.Z)**0, cirq.I),
    ((cirq.X - 1j * cirq.Y)**0, cirq.I),
))
def test_gate_expressions(expression, expected_result):
    assert_linear_combinations_are_equal(expression, expected_result)


@pytest.mark.parametrize('gates', (
    (cirq.X, cirq.T, cirq.T, cirq.X, cirq.Z),
    (cirq.CZ, cirq.XX, cirq.YY, cirq.ZZ),
    (cirq.TOFFOLI, cirq.TOFFOLI, cirq.FREDKIN),
def test_non_unitary_linear_combination_of_gates_has_no_unitary(terms):
    combination = cirq.LinearCombinationOfGates(terms)
    assert not cirq.has_unitary(combination)
    with pytest.raises((TypeError, ValueError)):
        _ = cirq.unitary(combination)
def test_unitary_linear_combination_of_gates_has_correct_unitary(
        terms, expected_unitary):
    combination = cirq.LinearCombinationOfGates(terms)
    assert cirq.has_unitary(combination)
    assert np.allclose(cirq.unitary(combination), expected_unitary)
Beispiel #15
0
def test_matrix(terms, expected_matrix):
    combination = cirq.LinearCombinationOfGates(terms)
    assert np.all(combination.matrix() == expected_matrix)
Beispiel #16
0
def test_num_qubits(terms, expected_num_qubits):
    combination = cirq.LinearCombinationOfGates(terms)
    assert combination.num_qubits() == expected_num_qubits