Example #1
0
def test_flatten_op_tree():
    operations = [
        Operation(Gate(), [NamedQubit(str(i))])
        for i in range(10)
    ]

    # Empty tree.
    assert list(op_tree.flatten_op_tree([[[]]])) == []

    # Just an item.
    assert list(op_tree.flatten_op_tree(operations[0])) == operations[:1]

    # Flat list.
    assert list(op_tree.flatten_op_tree(operations)) == operations

    # Tree.
    assert list(op_tree.flatten_op_tree((operations[0],
                                         operations[1:5],
                                         operations[5:]))) == operations

    # Bad trees.
    with pytest.raises(TypeError):
        _ = list(op_tree.flatten_op_tree(None))
    with pytest.raises(TypeError):
        _ = list(op_tree.flatten_op_tree(5))
    with pytest.raises(TypeError):
        _ = list(op_tree.flatten_op_tree([operations[0], (4,)]))
Example #2
0
def test_transform_bad_tree():
    with pytest.raises(TypeError):
        _ = list(op_tree.transform_op_tree(None))
    with pytest.raises(TypeError):
        _ = list(op_tree.transform_op_tree(5))
    with pytest.raises(TypeError):
        _ = list(op_tree.flatten_op_tree(op_tree.transform_op_tree([
            Operation(Gate(), [QubitId()]), (4,)
        ])))
Example #3
0
 def _unitary_(self) -> np.ndarray:
     mat = np.eye(2)
     qubit = named_qubit.NamedQubit('arbitrary')
     for op in op_tree.flatten_op_tree(self.default_decompose((qubit, ))):
         mat = protocols.unitary(op).dot(mat)
     return mat
Example #4
0
 def matrix(self) -> np.ndarray:
     mat = np.eye(2)
     qubit = raw_types.QubitId()
     for op in op_tree.flatten_op_tree(self.default_decompose((qubit, ))):
         mat = protocols.unitary(op).dot(mat)
     return mat
Example #5
0
 def matrix(self) -> np.ndarray:
     mat = np.eye(2)
     qubit = raw_types.QubitId()
     for op in op_tree.flatten_op_tree(self.default_decompose((qubit, ))):
         mat = cast(gate_features.KnownMatrix, op).matrix().dot(mat)
     return mat