Exemple #1
0
def test_parse_failures(url, msg):
    parsed_url = urllib.parse.urlparse(url)
    data = json.loads(parsed_url.fragment[len('circuit='):])

    with pytest.raises(ValueError, match=msg):
        _ = quirk_url_to_circuit(url)

    with pytest.raises(ValueError, match=msg):
        _ = quirk_json_to_circuit(data)
Exemple #2
0
def test_custom_circuit_gate():
    a, b, c, d, e = cirq.LineQubit.range(5)

    # Without name.
    assert_url_to_circuit_returns(
        '{"cols":[["~d3pq"],["Y"]],'
        '"gates":[{"id":"~d3pq","circuit":{"cols":[["H"],["•","X"]]}}]}',
        cirq.Circuit(
            cirq.H(a),
            cirq.X(b).controlled_by(a),
            cirq.Y(a),
        ),
    )

    # With name.
    assert_url_to_circuit_returns(
        '{"cols":[["~d3pq"],["Y"]],'
        '"gates":[{"id":"~d3pq","name":"test",'
        '"circuit":{"cols":[["H"],["•","X"]]}}]}',
        cirq.Circuit(
            cirq.H(a),
            cirq.X(b).controlled_by(a),
            cirq.Y(a),
        ),
    )

    # With internal input.
    assert_url_to_circuit_returns(
        '{"cols":[["~a5ls"]],"gates":[{"id":"~a5ls","circuit":{"cols":[["inputA1","+=A1"]]}}]}',
        cirq.Circuit(
            cirq.interop.quirk.QuirkArithmeticOperation('+=A1',
                                                        target=[b],
                                                        inputs=[[a]])),
    )

    # With external input.
    assert_url_to_circuit_returns(
        '{"cols":[["inputA1","~r79k"]],"gates":[{"id":"~r79k","circuit":{"cols":[["+=A1"]]}}]}',
        cirq.Circuit(
            cirq.interop.quirk.QuirkArithmeticOperation('+=A1',
                                                        target=[b],
                                                        inputs=[[a]])),
    )

    # With external control.
    assert_url_to_circuit_returns(
        '{"cols":[["•",1,"~r79k"]],"gates":[{"id":"~r79k",'
        '"circuit":{"cols":[["X"],["Y","Z"]]}}]}',
        cirq.Circuit(
            cirq.X(c).controlled_by(a),
            cirq.Y(c).controlled_by(a),
            cirq.Z(d).controlled_by(a)),
    )

    # With external and internal control.
    assert_url_to_circuit_returns(
        '{"cols":[["•",1,"~r79k"]],"gates":[{"id":"~r79k",'
        '"circuit":{"cols":[["X"],["⊕","Z"]]}}]}',
        cirq.Circuit(
            cirq.X(c).controlled_by(a),
            cirq.Y(c)**0.5,
            cirq.Z(d).controlled_by(a, c),
            cirq.Y(c)**-0.5,
        ),
    )

    # Broadcast input.
    assert_url_to_circuit_returns(
        '{"cols":[["~q1fh",1,1,"inputA2"]],"gates":[{"id":"~q1fh",'
        '"circuit":{"cols":[["+=A2"],[1,"+=A2"],[1,"+=A2"]]}}]}',
        cirq.Circuit(
            cirq.interop.quirk.QuirkArithmeticOperation('+=A2',
                                                        target=[a, b],
                                                        inputs=[[d, e]]),
            cirq.interop.quirk.QuirkArithmeticOperation('+=A2',
                                                        target=[b, c],
                                                        inputs=[[d, e]]),
            cirq.interop.quirk.QuirkArithmeticOperation('+=A2',
                                                        target=[b, c],
                                                        inputs=[[d, e]]),
        ),
    )

    # Nested custom gate.
    assert_url_to_circuit_returns(
        '{"cols":[["~gtnd"]],"gates":[{"id":"~ct36",'
        '"circuit":{"cols":[["X"],["X"]]}},{"id":"~gtnd",'
        '"circuit":{"cols":[["~ct36"],["~ct36"]]}}]}',
        cirq.Circuit(cirq.X(a)) * 4,
    )

    # Nested custom gate wrong order.
    with pytest.raises(ValueError, match='Unrecognized column entry'):
        _ = quirk_json_to_circuit({
            "cols": [["~gtnd"]],
            "gates": [
                {
                    "id": "~gtnd",
                    "circuit": {
                        "cols": [["~ct36"], ["~ct36"]]
                    }
                },
                {
                    "id": "~ct36",
                    "circuit": {
                        "cols": [["X"], ["X"]]
                    }
                },
            ],
        })