Exemple #1
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(xrange(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)
Exemple #2
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(xrange(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)