def __new__(cls, *args, **assumptions): if assumptions.get('evaluate') is False: args = map(_sympify, args) obj = Expr.__new__(cls, *args, **assumptions) obj.is_commutative = all(arg.is_commutative for arg in args) return obj if len(args)==0: return cls.identity if len(args)==1: return _sympify(args[0]) c_part, nc_part, order_symbols = cls.flatten(map(_sympify, args)) if len(c_part) + len(nc_part) <= 1: if c_part: obj = c_part[0] elif nc_part: obj = nc_part[0] else: obj = cls.identity else: obj = Expr.__new__(cls, *(c_part + nc_part), **assumptions) obj.is_commutative = not nc_part if order_symbols is not None: obj = C.Order(obj, *order_symbols) return obj
def _eval_args(cls, args): targets = args[0] if not isinstance(targets, (list, tuple, Tuple)): targets = (targets,) targets = Gate._eval_args(targets) _validate_targets_controls(targets) mat = args[1] if not isinstance(mat, Matrix): raise TypeError("Matrix expected, got: %r" % mat) dim = 2 ** len(targets) if not all([dim == shape for shape in mat.shape]): raise IndexError("Number of targets must match the matrix size: %r %r" % (targets, mat)) return (targets, mat)
def _eval_args(cls, args): targets = args[0] if not isinstance(targets, (list, tuple, Tuple)): targets = (targets, ) targets = Gate._eval_args(targets) _validate_targets_controls(targets) mat = args[1] if not isinstance(mat, Matrix): raise TypeError('Matrix expected, got: %r' % mat) dim = 2**len(targets) if not all([dim == shape for shape in mat.shape]): raise IndexError( 'Number of targets must match the matrix size: %r %r' %\ (targets, mat) ) return (targets, mat)
def _eval_args(cls, args): targets = args[0] if not ordered_iter(targets, include=Tuple): targets = (targets,) targets = Gate._eval_args(targets) _validate_targets_controls(targets) mat = args[1] if not isinstance(mat, Matrix): raise TypeError('Matrix expected, got: %r' % mat) dim = 2**len(targets) if not all([dim == shape for shape in mat.shape]): raise IndexError( 'Number of targets must match the matrix size: %r %r' %\ (targets, mat) ) return (targets, mat)
def eval_controls(self, qubit): """Return True/False to indicate if the controls are satisfied.""" return all([qubit[bit] == self.control_value for bit in self.controls])
def eval_controls(self, qubit): """Return True/False to indicate if the controls are satisfied.""" return all([qubit[bit]==self.control_value for bit in self.controls])
def independent(h): return all(not monom[0] for monom in h.monoms)