예제 #1
0
 def __init__(self, base, exponent):
     r'''
     Tensor exponentiation to any natural number exponent.
     '''
     Operation.__init__(self, TENSOR_EXP, (base, exponent))
     self.base = self.operands[0]
     self.exponent = self.operands[1]
예제 #2
0
 def __init__(self, target_gate):
     '''
     Create a Target operation with the given target_gate as the type
     of the gate for the target (e.g., X for CNOT and Z for Controlled-Z).
     '''
     Operation.__init__(self, TARGET, target_gate)
     self.target_gate = target_gate
예제 #3
0
 def __init__(self, label, index):
     '''
     \alpha_l
     '''
     Operation.__init__(self, SUB_INDEXED, [label, index])
     self.label = label
     self.index = index
예제 #4
0
    def __init__(self, tensor, shape=None):
        '''
        Create a Circuit either with a dense tensor (list of lists ... of lists) or
        with a dictionary mapping pairs of indices to Expression elements or blocks,
        composing an ExpressionTensor.
        '''
        from .common import PASS
        if not isinstance(tensor, dict):
            # remove identity gates -- these are implicit
            tensor, _ = ExpressionTensor.TensorDictFromIterables(tensor)
        fixed_shape = (shape is not None)
        if not fixed_shape:
            shape = (0, 0)
        for idx in list(tensor.keys()):
            if len(idx) != 2:
                raise ValueError(
                    'Circuit operand must be a 2-D ExpressionTensor')
            if not fixed_shape:
                shape = (max(shape[0], idx[0] + 1), max(shape[1], idx[1] + 1))
            if tensor[idx] == PASS:
                tensor.pop(idx)
        self.tensor = ExpressionTensor(tensor, shape)
        self.shape = self.tensor.shape
        Operation.__init__(self, CIRCUIT, [self.tensor])
        if len(self.shape) != 2:
            raise ValueError('Circuit operand must be a 2-D ExpressionTensor')
        # For each row of each nested sub-tensor (including the top level),
        # determine which sub-tensor, if there are any, has the deepest nesting.
        # This will impact how we iterate over nested rows to flatten the display of a nested tensors.
        tensor = self.tensor
        self.deepestNestedTensorAlongRow = dict(
        )  # map nested tensor (including self) to a list that indicates the deepest nested tensor per row

        def determineDeepestNestedTensors(tensor):
            '''
            Determine and set the deepest nested tensor along each row of tensor,
            applying this recursively for sub-tensors, and return the depth of this tensor.
            '''
            maxDepth = 1
            self.deepestNestedTensorAlongRow[
                tensor] = deepestNestedTensorAlongRow = []
            for row in range(tensor.shape[0]):
                deepestNestedTensor = None
                maxDepthAlongRow = 1
                for col in range(tensor.shape[1]):
                    if (row, col) in tensor:
                        cell = tensor[row, col]
                        if isinstance(cell, ExpressionTensor):
                            subDepth = determineDeepestNestedTensors(cell)
                            maxDepthAlongRow = max(maxDepthAlongRow,
                                                   subDepth + 1)
                            if deepestNestedTensor is None or subDepth > maxDepthAlongRow:
                                deepestNestedTensor = cell
                maxDepth = max(maxDepth, maxDepthAlongRow + 1)
                deepestNestedTensorAlongRow.append(deepestNestedTensor)
            return maxDepth

        determineDeepestNestedTensors(self.tensor)
예제 #5
0
 def __init__(self, expr_tensor):
     Operation.__init__(self, MATRIX, expr_tensor)
     self.tensor = self.operands
     if not isinstance(self.tensor, ExpressionTensor):
         raise ImproperMatrix(
             'Matrix must be given an ExpressionTensor for its operands')
     if len(self.tensor.shape) != 2:
         raise ImproperMatrix(
             'Matrix must be given a 2-dimensional ExpressionTensor')
     self.nrows = self.tensor.shape[0]
     self.ncolumns = self.tensor.shape[1]
예제 #6
0
 def __init__(self, n):
     '''
     Create some SU(n), the special unitary of degree n.
     '''
     Operation.__init__(self, SPECIAL_UNITARY, n)
     self.operand = n
예제 #7
0
 def __init__(self, state):
     '''
     Create a INPUT operation with the given input state.
     '''
     Operation.__init__(self, OUTPUT, state)
     self.state = state
예제 #8
0
 def __init__(self, number):
     '''
     Create a multi-wire.
     '''
     Operation.__init__(self, MULTI_WIRE, number)
     self.number = number
예제 #9
0
 def __init__(self, label):
     Operation.__init__(self, BRA, label)
     self.label = label
예제 #10
0
 def __init__(self, mapExpr):
     Operation.__init__(self, DOMAIN, [mapExpr])
예제 #11
0
 def __init__(self, eps):
     '''
     P_fail(eps)
     '''
     Operation.__init__(self, P_FAIL, eps)
예제 #12
0
 def __init__(self, eps):
     '''
     P_success(eps)
     '''
     Operation.__init__(self, P_SUCCESS, eps)
예제 #13
0
 def __init__(self, U, t):
     '''
     Phase estimator circuit for Unitary U and t register qubits.
     '''
     Operation.__init__(self, PHASE_ESTIMATION, (U, t))
예제 #14
0
 def __init__(self, event, random_variable):
     Operation.__init__(self, PROB, [event, random_variable])
     self.event = event
     self.random_variable = random_variable
예제 #15
0
 def __init__(self, ket):
     Operation.__init__(self, MEAS, ket)
     self.ket = ket
예제 #16
0
 def __init__(self, label, size):
     Operation.__init__(self, REGISTER_KET, [label, size])
     self.label = label
     self.size = size  # size of the register
예제 #17
0
 def __init__(self, angle1, angle2):
     Operation.__init__(self, ANGULAR_DIFFERENCE, (angle1, angle2))
     self.angle1 = angle1
     self.angle2 = angle2
예제 #18
0
 def __init__(self, gate_operation):
     '''
     Create a quantum circuit gate performing the given operation.
     '''
     Operation.__init__(self, GATE, gate_operation)
     self.gate_operation = gate_operation
예제 #19
0
 def __init__(self, n):
     '''
     QFT circuit for n qubits.
     '''
     Operation.__init__(self, INV_FT, n)
     self.nqubits = n
예제 #20
0
 def __init__(self, map_expr):
     Operation.__init__(self, CODOMAIN, [map_expr])