Example #1
0
def test_freeze_op_tree():
    operations = [
        cirq.GateOperation(cirq.SingleQubitGate(), [cirq.NamedQubit(str(i))])
        for i in range(10)
    ]

    # Empty tree.
    assert cirq.freeze_op_tree([[[]]]) == (((), ), )

    # Just an item.
    assert cirq.freeze_op_tree(operations[0]) == operations[0]

    # Flat list.
    assert cirq.freeze_op_tree(operations) == tuple(operations)

    # Tree.
    assert (cirq.freeze_op_tree(
        (operations[0], (operations[i] for i in range(1, 5)),
         operations[5:])) == (operations[0], tuple(operations[1:5]),
                              tuple(operations[5:])))

    # Bad trees.
    with pytest.raises(TypeError):
        cirq.freeze_op_tree(None)
    with pytest.raises(TypeError):
        cirq.freeze_op_tree(5)
    with pytest.raises(TypeError):
        _ = cirq.freeze_op_tree([operations[0], (4, )])
Example #2
0
def test_freeze_op_tree():
    operations = [
        cirq.GateOperation(cirq.Gate(), [cirq.NamedQubit(str(i))])
        for i in range(10)
    ]

    # Empty tree.
    assert cirq.freeze_op_tree([[[]]]) == (((),),)

    # Just an item.
    assert cirq.freeze_op_tree(operations[0]) == operations[0]

    # Flat list.
    assert cirq.freeze_op_tree(operations) == tuple(operations)

    # Tree.
    assert (
            cirq.freeze_op_tree(
            (operations[0], (operations[i] for i in range(1, 5)),
             operations[5:])) ==
        (operations[0], tuple(operations[1:5]), tuple(operations[5:])))

    # Bad trees.
    with pytest.raises(TypeError):
        cirq.freeze_op_tree(None)
    with pytest.raises(TypeError):
        cirq.freeze_op_tree(5)
    with pytest.raises(TypeError):
        _ = cirq.freeze_op_tree([operations[0], (4,)])
def test_child_class():
    class Impl(cirq.ReversibleCompositeGate):
        def default_decompose(self, qubits):
            yield _FlipGate(1)(*qubits)
            yield _FlipGate(2)(*qubits), _FlipGate(3)(*qubits)

    gate = Impl()
    reversed_gate = gate.inverse()
    assert gate is reversed_gate.inverse()

    q = cirq.QubitId()
    assert (cirq.freeze_op_tree(gate.default_decompose(
        [q])) == (_FlipGate(1)(q), (_FlipGate(2)(q), _FlipGate(3)(q))))
    assert (cirq.freeze_op_tree(reversed_gate.default_decompose(
        [q])) == ((_FlipGate(~3)(q), _FlipGate(~2)(q)), _FlipGate(~1)(q)))
def test_child_class():

    class Impl(cirq.ReversibleCompositeGate):
        def default_decompose(self, qubits):
            yield _FlipGate(1)(*qubits)
            yield _FlipGate(2)(*qubits), _FlipGate(3)(*qubits)

    gate = Impl()
    reversed_gate = gate.inverse()
    assert gate is reversed_gate.inverse()

    q = cirq.QubitId()
    assert (
        cirq.freeze_op_tree(gate.default_decompose([q])) ==
        (_FlipGate(1)(q), (_FlipGate(2)(q), _FlipGate(3)(q))))
    assert (
        cirq.freeze_op_tree(reversed_gate.default_decompose([q])) ==
        ((_FlipGate(~3)(q), _FlipGate(~2)(q)), _FlipGate(~1)(q)))
Example #5
0
def test_child_class():
    class Impl(cirq.ReversibleCompositeGate):
        def default_decompose(self, qubits):
            yield _FlipGate(1)(*qubits)
            yield _FlipGate(2)(*qubits), _FlipGate(3)(*qubits)

    gate = Impl()
    reversed_gate = gate**-1
    assert gate is reversed_gate**-1
    with pytest.raises(TypeError):
        _ = gate**0.5
    with pytest.raises(TypeError):
        _ = reversed_gate**0.5

    q = cirq.NamedQubit('q')
    assert (cirq.freeze_op_tree(gate.default_decompose(
        [q])) == (_FlipGate(1)(q), (_FlipGate(2)(q), _FlipGate(3)(q))))
    assert (cirq.freeze_op_tree(reversed_gate.default_decompose(
        [q])) == ((_FlipGate(~3)(q), _FlipGate(~2)(q)), _FlipGate(~1)(q)))
Example #6
0
def test_op_tree_control():
    gs = [cirq.SingleQubitGate() for _ in range(10)]
    op_tree = [
        cirq.GateOperation(gs[i], [cirq.NamedQubit(str(i))]) for i in range(10)
    ]
    controls = cirq.LineQubit.range(2)
    controlled_op_tree = cirq.protocols.control(op_tree, controls)
    expected = [
        cirq.ControlledOperation(
            controls, cirq.GateOperation(gs[i], [cirq.NamedQubit(str(i))]))
        for i in range(10)
    ]
    assert cirq.freeze_op_tree(controlled_op_tree) == tuple(expected)
Example #7
0
 def skip_tree_freeze(root):
     return cirq.freeze_op_tree(
         cirq.transform_op_tree(root, iter_transformation=skip_first))
Example #8
0
 def move_tree_left_freeze(root):
     return cirq.freeze_op_tree(cirq.transform_op_tree(root, move_left))
 def rev_freeze(root):
     return cirq.freeze_op_tree(cirq.inverse(root))
Example #10
0
 def skip_tree_freeze(root):
     return cirq.freeze_op_tree(
         cirq.transform_op_tree(root, iter_transformation=skip_first))
Example #11
0
 def move_tree_left_freeze(root):
     return cirq.freeze_op_tree(
         cirq.transform_op_tree(root, move_left))
 def rev_freeze(root):
     return cirq.freeze_op_tree(cirq.inverse(root))