Ejemplo n.º 1
0
 def _eval_args_with_nqubits(cls, number, nqubits):
     need = bitcount(abs(number))
     if nqubits < need:
         raise ValueError('cannot represent %s with %s bits' %
                          (number, nqubits))
     qubit_values = [(number >> i) & 1 for i in reversed(range(nqubits))]
     return QubitState._eval_args(qubit_values)
Ejemplo n.º 2
0
 def _eval_args(cls, args, nqubits=None):
     # The case of a QubitState instance
     if len(args) == 1 and isinstance(args[0], QubitState):
         return QubitState._eval_args(args)
     # otherwise, args should be integer
     elif not all((isinstance(a, (int, Integer)) for a in args)):
         raise ValueError('values must be integers, got (%s)' %
                          (tuple(type(a) for a in args), ))
     # use nqubits if specified
     if nqubits is not None:
         if not isinstance(nqubits, (int, Integer)):
             raise ValueError('nqubits must be an integer, got (%s)' %
                              type(nqubits))
         if len(args) != 1:
             raise ValueError(
                 'too many positional arguments (%s). should be (number, nqubits=n)'
                 % (args, ))
         return cls._eval_args_with_nqubits(args[0], nqubits)
     # For a single argument, we construct the binary representation of
     # that integer with the minimal number of bits.
     if len(args) == 1 and args[0] > 1:
         #rvalues is the minimum number of bits needed to express the number
         rvalues = reversed(range(bitcount(abs(args[0]))))
         qubit_values = [(args[0] >> i) & 1 for i in rvalues]
         return QubitState._eval_args(qubit_values)
     # For two numbers, the second number is the number of bits
     # on which it is expressed, so IntQubit(0,5) == |00000>.
     elif len(args) == 2 and args[1] > 1:
         return cls._eval_args_with_nqubits(args[0], args[1])
     else:
         return QubitState._eval_args(args)
Ejemplo n.º 3
0
 def _eval_args_with_nqubits(cls, number, nqubits):
     need = bitcount(abs(number))
     if nqubits < need:
         raise ValueError(
             'cannot represent %s with %s bits' % (number, nqubits))
     qubit_values = [(number >> i) & 1 for i in reversed(range(nqubits))]
     return QubitState._eval_args(qubit_values)
Ejemplo n.º 4
0
 def _eval_args(cls, args, nqubits=None):
     # The case of a QubitState instance
     if len(args) == 1 and isinstance(args[0], QubitState):
         return QubitState._eval_args(args)
     # otherwise, args should be integer
     elif not all((isinstance(a, (int, Integer)) for a in args)):
         raise ValueError('values must be integers, got (%s)' % (tuple(type(a) for a in args),))
     # use nqubits if specified
     if nqubits is not None:
         if not isinstance(nqubits, (int, Integer)):
             raise ValueError('nqubits must be an integer, got (%s)' % type(nqubits))
         if len(args) != 1:
             raise ValueError(
                 'too many positional arguments (%s). should be (number, nqubits=n)' % (args,))
         return cls._eval_args_with_nqubits(args[0], nqubits)
     # For a single argument, we construct the binary representation of
     # that integer with the minimal number of bits.
     if len(args) == 1 and args[0] > 1:
         #rvalues is the minimum number of bits needed to express the number
         rvalues = reversed(range(bitcount(abs(args[0]))))
         qubit_values = [(args[0] >> i) & 1 for i in rvalues]
         return QubitState._eval_args(qubit_values)
     # For two numbers, the second number is the number of bits
     # on which it is expressed, so IntQubit(0,5) == |00000>.
     elif len(args) == 2 and args[1] > 1:
         return cls._eval_args_with_nqubits(args[0], args[1])
     else:
         return QubitState._eval_args(args)
Ejemplo n.º 5
0
 def _eval_args(cls, args):
     # The case of a QubitState instance
     if len(args) == 1 and isinstance(args[0], QubitState):
         return QubitState._eval_args(args)
     # For a single argument, we construct the binary representation of
     # that integer with the minimal number of bits.
     if len(args) == 1 and args[0] > 1:
         # rvalues is the minimum number of bits needed to express the number
         rvalues = reversed(range(bitcount(abs(args[0]))))
         qubit_values = [(args[0] >> i) & 1 for i in rvalues]
         return QubitState._eval_args(qubit_values)
     # For two numbers, the second number is the number of bits
     # on which it is expressed, so IntQubit(0,5) == |00000>.
     elif len(args) == 2 and args[1] > 1:
         need = bitcount(abs(args[0]))
         if args[1] < need:
             raise ValueError("cannot represent %s with %s bits" % (args[0], args[1]))
         qubit_values = [(args[0] >> i) & 1 for i in reversed(range(args[1]))]
         return QubitState._eval_args(qubit_values)
     else:
         return QubitState._eval_args(args)
Ejemplo n.º 6
0
 def _eval_args(cls, args):
     # The case of a QubitState instance
     if len(args) == 1 and isinstance(args[0], QubitState):
         return QubitState._eval_args(args)
     # For a single argument, we construct the binary representation of
     # that integer with the minimal number of bits.
     if len(args) == 1 and args[0] > 1:
         #rvalues is the minimum number of bits needed to express the number
         rvalues = reversed(range(bitcount(abs(args[0]))))
         qubit_values = [(args[0] >> i) & 1 for i in rvalues]
         return QubitState._eval_args(qubit_values)
     # For two numbers, the second number is the number of bits
     # on which it is expressed, so IntQubit(0,5) == |00000>.
     elif len(args) == 2 and args[1] > 1:
         need = bitcount(abs(args[0]))
         if args[1] < need:
             raise ValueError(
                 'cannot represent %s with %s bits' % (args[0], args[1]))
         qubit_values = [(args[0] >> i) & 1 for i in reversed(range(args[1]))]
         return QubitState._eval_args(qubit_values)
     else:
         return QubitState._eval_args(args)