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]
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
def __init__(self, label, index): ''' \alpha_l ''' Operation.__init__(self, SUB_INDEXED, [label, index]) self.label = label self.index = index
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)
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]
def __init__(self, n): ''' Create some SU(n), the special unitary of degree n. ''' Operation.__init__(self, SPECIAL_UNITARY, n) self.operand = n
def __init__(self, state): ''' Create a INPUT operation with the given input state. ''' Operation.__init__(self, OUTPUT, state) self.state = state
def __init__(self, number): ''' Create a multi-wire. ''' Operation.__init__(self, MULTI_WIRE, number) self.number = number
def __init__(self, label): Operation.__init__(self, BRA, label) self.label = label
def __init__(self, mapExpr): Operation.__init__(self, DOMAIN, [mapExpr])
def __init__(self, eps): ''' P_fail(eps) ''' Operation.__init__(self, P_FAIL, eps)
def __init__(self, eps): ''' P_success(eps) ''' Operation.__init__(self, P_SUCCESS, eps)
def __init__(self, U, t): ''' Phase estimator circuit for Unitary U and t register qubits. ''' Operation.__init__(self, PHASE_ESTIMATION, (U, t))
def __init__(self, event, random_variable): Operation.__init__(self, PROB, [event, random_variable]) self.event = event self.random_variable = random_variable
def __init__(self, ket): Operation.__init__(self, MEAS, ket) self.ket = ket
def __init__(self, label, size): Operation.__init__(self, REGISTER_KET, [label, size]) self.label = label self.size = size # size of the register
def __init__(self, angle1, angle2): Operation.__init__(self, ANGULAR_DIFFERENCE, (angle1, angle2)) self.angle1 = angle1 self.angle2 = angle2
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
def __init__(self, n): ''' QFT circuit for n qubits. ''' Operation.__init__(self, INV_FT, n) self.nqubits = n
def __init__(self, map_expr): Operation.__init__(self, CODOMAIN, [map_expr])