Exemplo n.º 1
0
def test_non_physical_operations():
    with pytest.raises(NotImplementedError, match="unphysical operation"):
        _ = quirk_url_to_circuit(
            'https://algassert.com/quirk#circuit={"cols":['
            '["__error__"]]}')
    with pytest.raises(NotImplementedError, match="unphysical operation"):
        _ = quirk_url_to_circuit(
            'https://algassert.com/quirk#circuit={"cols":['
            '["__unstable__UniversalNot"]]}')
Exemplo n.º 2
0
def test_swap():
    a, b, c = cirq.LineQubit.range(3)
    assert_url_to_circuit_returns('{"cols":[["Swap","Swap"]]}',
                                  cirq.Circuit(cirq.SWAP(a, b)))
    assert_url_to_circuit_returns('{"cols":[["Swap","X","Swap"]]}',
                                  cirq.Circuit(cirq.SWAP(a, c), cirq.X(b)))

    with pytest.raises(ValueError, match='number of swap gates'):
        _ = quirk_url_to_circuit(
            'https://algassert.com/quirk#circuit={"cols":[['
            '"Swap"]]}')
    with pytest.raises(ValueError, match='number of swap gates'):
        _ = quirk_url_to_circuit(
            'https://algassert.com/quirk#circuit={"cols":[['
            '"Swap","Swap","Swap"]]}')
Exemplo n.º 3
0
def test_with_registers():
    circuit = quirk_url_to_circuit(
        'https://algassert.com/quirk#circuit={"cols":'
        '['
        '[{"id":"setA","arg":3}],'
        '["+=AB3",1,1,"inputB2"]'
        ']}')
    op = cast(cirq.ArithmeticOperation, circuit[0].operations[0])

    with pytest.raises(ValueError, match='number of registers'):
        _ = op.with_registers()

    with pytest.raises(ValueError, match='first register.*mutable target'):
        _ = op.with_registers(1, 2, 3)

    op2 = op.with_registers([], 5, 5)
    np.testing.assert_allclose(cirq.unitary(cirq.Circuit(op2)),
                               np.array([[1]]),
                               atol=1e-8)

    op2 = op.with_registers([*cirq.LineQubit.range(3)], 5, 5)
    np.testing.assert_allclose(cirq.final_wavefunction(cirq.Circuit(op2),
                                                       initial_state=0),
                               cirq.one_hot(index=25 % 8,
                                            shape=8,
                                            dtype=np.complex64),
                               atol=1e-8)
Exemplo n.º 4
0
def test_arithmetic_comparison_gates():
    with pytest.raises(ValueError, match='Missing input'):
        _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={"cols":'
                                 '[["^A<B"]]}')
    assert_url_to_circuit_returns('{"cols":[["^A<B","inputA2",1,"inputB2"]]}',
                                  diagram="""
0: ───Quirk(^A<B)───
      │
1: ───A0────────────
      │
2: ───A1────────────
      │
3: ───B0────────────
      │
4: ───B1────────────
        """,
                                  maps={
                                      0b_0_00_10: 0b_1_00_10,
                                      0b_1_00_10: 0b_0_00_10,
                                      0b_0_11_10: 0b_0_11_10,
                                      0b_0_10_10: 0b_0_10_10,
                                      0b_0_01_10: 0b_1_01_10,
                                  })

    assert_url_to_circuit_returns('{"cols":[["^A>B","inputA2",1,"inputB2"]]}',
                                  maps={
                                      0b_0_11_10: 0b_1_11_10,
                                      0b_0_10_10: 0b_0_10_10,
                                      0b_0_01_10: 0b_0_01_10,
                                  })

    assert_url_to_circuit_returns('{"cols":[["^A>=B","inputA2",1,"inputB2"]]}',
                                  maps={
                                      0b_0_11_10: 0b_1_11_10,
                                      0b_0_10_10: 0b_1_10_10,
                                      0b_0_01_10: 0b_0_01_10,
                                  })

    assert_url_to_circuit_returns('{"cols":[["^A<=B","inputA2",1,"inputB2"]]}',
                                  maps={
                                      0b_0_11_10: 0b_0_11_10,
                                      0b_0_10_10: 0b_1_10_10,
                                      0b_0_01_10: 0b_1_01_10,
                                  })

    assert_url_to_circuit_returns('{"cols":[["^A=B","inputA2",1,"inputB2"]]}',
                                  maps={
                                      0b_0_11_10: 0b_0_11_10,
                                      0b_0_10_10: 0b_1_10_10,
                                      0b_0_01_10: 0b_0_01_10,
                                  })

    assert_url_to_circuit_returns('{"cols":[["^A!=B","inputA2",1,"inputB2"]]}',
                                  maps={
                                      0b_0_11_10: 0b_1_11_10,
                                      0b_0_10_10: 0b_0_10_10,
                                      0b_0_01_10: 0b_1_01_10,
                                  })
Exemplo n.º 5
0
def test_repr():
    circuit = quirk_url_to_circuit(
        'https://algassert.com/quirk#circuit={"cols":'
        '['
        '[{"id":"setA","arg":3}],'
        '["+=AB3",1,1,"inputB2"]'
        ']}')
    op = circuit[0].operations[0]
    cirq.testing.assert_equivalent_repr(op)
Exemplo n.º 6
0
def test_modular_arithmetic_modulus_size():
    with pytest.raises(ValueError, match='too small for modulus'):
        _ = quirk_url_to_circuit(
            'https://algassert.com/quirk#circuit={"cols":['
            '[{"id":"setR","arg":17}],["incmodR4"]]}')

    assert_url_to_circuit_returns(
        '{"cols":[[{"id":"setR","arg":16}],["incmodR4"]]}')
    assert_url_to_circuit_returns(
        '{"cols":[[{"id":"setR","arg":15}],["incmodR4"]]}')

    with pytest.raises(ValueError, match='too small for modulus'):
        _ = quirk_url_to_circuit(
            'https://algassert.com/quirk#circuit={"cols":['
            '["incmodR2",1,"inputR3"]]}')

    assert_url_to_circuit_returns('{"cols":[["incmodR3",1,1,"inputR3"]]}')

    assert_url_to_circuit_returns('{"cols":[["incmodR4",1,1,1,"inputR3"]]}')

    assert_url_to_circuit_returns('{"cols":[["incmodR2",1,"inputR2"]]}')
Exemplo n.º 7
0
def test_repr():
    circuit = quirk_url_to_circuit(
        'https://algassert.com/quirk#circuit={"cols":'
        '['
        '[{"id":"setA","arg":3}],'
        '["+=AB3",1,1,"inputB2"]'
        ']}')
    op = circuit[0].operations[0]
    cirq.testing.assert_equivalent_repr(op)

    cirq.testing.assert_equivalent_repr(
        cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
            '+=A2', cirq.LineQubit.range(2), [cirq.LineQubit.range(2, 5)]))
Exemplo n.º 8
0
def test_not_implemented_gates():
    # This test mostly exists to ensure the gates are tested if added.

    for k in ["X^⌈t⌉", "X^⌈t-¼⌉", "Counting4", "Uncounting4", ">>t3", "<<t3"]:
        with pytest.raises(NotImplementedError, match="discrete parameter"):
            _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={'
                                     '"cols":[["' + k + '"]]}')

    for k in ["add3", "sub3", "c+=ab4", "c-=ab4"]:
        with pytest.raises(NotImplementedError, match="deprecated"):
            _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={'
                                     '"cols":[["' + k + '"]]}')

    for k in ["X", "Y", "Z"]:
        with pytest.raises(NotImplementedError, match="feedback"):
            _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={"'
                                     'cols":[["' + k +
                                     'DetectControlReset"]]}')

    for k in ["|0⟩⟨0|", "|1⟩⟨1|", "|+⟩⟨+|", "|-⟩⟨-|", "|X⟩⟨X|", "|/⟩⟨/|", "0"]:
        with pytest.raises(NotImplementedError, match="postselection"):
            _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={'
                                     '"cols":[["' + k + '"]]}')
Exemplo n.º 9
0
def assert_url_to_circuit_returns(
        json_text: str,
        circuit: 'cirq.Circuit' = None,
        *,
        unitary: Optional[np.ndarray] = None,
        diagram: Optional[str] = None,
        output_amplitudes_from_quirk: Optional[List[Dict[str, float]]] = None,
        maps: Optional[Dict[int, int]] = None):
    """
    Args:
        json_text: The part of the quirk URL after "#circuit=".
        circuit: The optional expected circuit. If specified and not
            equal to the parsed circuit, an assertion fails.
        unitary: The optional expected unitary of the circuit. If specified
            and the parsed circuit has a different unitary, an assertion fails.
        diagram: The optional expected circuit diagram. If specified and the
            parsed circuit has a different diagram, an assertion fails.
        output_amplitudes_from_quirk: Optional data copied from Quirk's "export
            simulation data" function, for comparison to Cirq's simulator
            results. If specified and the output from the simulation differs
            from this data (after accounting for differences in endian-ness),
            an assertion fails.
        maps: Optional dictionary of test computational basis input states and
            the output computational basis state that they should be mapped to.
            If any state is mapped to the wrong thing, an assertion fails. Note
            that the states are specified using Quirk's little endian
            convention, meaning that the last bit of a binary literal will refer
            to the last qubit's value instead of vice versa.
    """
    parsed = quirk_url_to_circuit(
        f'https://algassert.com/quirk#circuit={json_text}')

    if diagram is not None:
        cirq.testing.assert_has_diagram(parsed, diagram)

    if circuit is not None:
        cirq.testing.assert_same_circuits(parsed, circuit)

    if unitary is not None:
        np.testing.assert_allclose(cirq.unitary(parsed), unitary, atol=1e-8)

    if output_amplitudes_from_quirk is not None:
        expected = np.array([
            float(e['r']) + 1j * float(e['i'])
            for e in output_amplitudes_from_quirk
        ])

        np.testing.assert_allclose(
            cirq.final_wavefunction(
                parsed,
                # Match Quirk's endian-ness for comparison purposes.
                qubit_order=sorted(parsed.all_qubits(), reverse=True),
            ),
            expected,
            atol=1e-8)

    if maps:
        keys = sorted(maps.keys())
        actual_map = _sparse_computational_basis_map(keys, parsed)
        for k in keys:
            assert actual_map.get(k) == maps[k], (
                f'{_bin_dec(k)} was mapped to '
                f'{_bin_dec(actual_map.get(k))} '
                f'instead of {_bin_dec(maps[k])}.')
Exemplo n.º 10
0
def test_input_rotation_cells_repr():
    circuit = quirk_url_to_circuit('http://algassert.com/quirk#circuit='
                                   '{"cols":[["•","X^(-A/2^n)","inputA2"]]}')
    op = circuit[0].operations[0]
    cirq.testing.assert_equivalent_repr(op)
Exemplo n.º 11
0
def test_input_rotation_cells():
    with pytest.raises(ValueError, match='classical constant'):
        _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={"cols":'
                                 '[["Z^(A/2^n)",{"id":"setA","arg":3}]]}')
    with pytest.raises(ValueError, match="Missing input 'a'"):
        _ = quirk_url_to_circuit('https://algassert.com/quirk#circuit={"cols":'
                                 '[["X^(A/2^n)"]]}')

    assert_url_to_circuit_returns(
        '{"cols":[["Z^(A/2^n)","inputA2"]]}',
        diagram="""
0: ───Z^(A/2^2)───
      │
1: ───A0──────────
      │
2: ───A1──────────
        """,
        unitary=np.diag([1, 1, 1, 1, 1j**0, 1j**0.5, 1j**1, 1j**1.5]))
    assert_url_to_circuit_returns('{"cols":[["Z^(-A/2^n)","inputA1"]]}',
                                  unitary=np.diag([1, 1, 1, -1j]))

    assert_url_to_circuit_returns(
        '{"cols":[["H"],["X^(A/2^n)","inputA2"],["H"]]}',
        unitary=np.diag([1, 1, 1, 1, 1j**0, 1j**0.5, 1j**1, 1j**1.5]))
    assert_url_to_circuit_returns(
        '{"cols":[["H"],["X^(-A/2^n)","inputA2"],["H"]]}',
        unitary=np.diag([1, 1, 1, 1, 1j**0, 1j**-0.5, 1j**-1, 1j**-1.5]))

    assert_url_to_circuit_returns(
        '{"cols":[["X^-½"],["Y^(A/2^n)","inputA2"],["X^½"]]}',
        unitary=np.diag([1, 1, 1, 1, 1j**0, 1j**0.5, 1j**1, 1j**1.5]))
    assert_url_to_circuit_returns(
        '{"cols":[["X^-½"],["Y^(-A/2^n)","inputA2"],["X^½"]]}',
        unitary=np.diag([1, 1, 1, 1, 1j**0, 1j**-0.5, 1j**-1, 1j**-1.5]))

    assert_url_to_circuit_returns('{"cols":[["•","Z^(A/2^n)","inputA2"]]}',
                                  diagram="""
0: ───@───────────
      │
1: ───Z^(A/2^2)───
      │
2: ───A0──────────
      │
3: ───A1──────────
        """,
                                  unitary=np.diag([1 + 0j] * 13 +
                                                  [1j**0.5, 1j, 1j**1.5]))

    assert_url_to_circuit_returns('{"cols":[["X^(-A/2^n)","inputA2"]]}',
                                  diagram="""
0: ───X^(-A/2^2)───
      │
1: ───A0───────────
      │
2: ───A1───────────
        """)

    assert_url_to_circuit_returns('{"cols":[["•","X^(-A/2^n)","inputA2"]]}',
                                  diagram="""
0: ───@────────────
      │
1: ───X^(-A/2^2)───
      │
2: ───A0───────────
      │
3: ───A1───────────
        """)

    assert_url_to_circuit_returns(
        '{"cols":[["Z^(A/2^n)","inputA1","inputB1"],[1,1,"Z"],[1,1,"Z"]]}',
        unitary=np.diag([1, 1, 1, 1, 1, 1, 1j, 1j]))