Ejemplo n.º 1
0
 def __init__(self,
              num_qubits: int,
              state: str = 'zero',
              state_vector: Optional[Union[np.ndarray, StateFn]] = None,
              circuit: Optional[QuantumCircuit] = None) -> None:
     """
     Args:
         num_qubits: Number of qubits, has a minimum value of 1.
         state: Use a predefined state of ('zero' | 'uniform' | 'random')
         state_vector: An optional vector of ``complex`` or ``float`` representing the state as
             a probability distribution which will be normalized to a total probability of 1
             when initializing the qubits. The length of the vector must be :math:`2^q`, where
             :math:`q` is the *num_qubits* value. When provided takes precedence over *state*.
         circuit: A quantum circuit for the desired initial state. When provided takes
             precedence over both *state_vector* and *state*.
     Raises:
         AquaError: invalid input
     """
     validate_min('num_qubits', num_qubits, 1)
     validate_in_set('state', state, {'zero', 'uniform', 'random'})
     super().__init__()
     self._num_qubits = num_qubits
     self._state = state
     size = np.power(2, self._num_qubits)
     self._circuit = None
     if isinstance(state_vector, StateFn):
         state_vector = state_vector.to_matrix()
     # pylint: disable=comparison-with-callable
     if circuit is not None:
         if circuit.width() != num_qubits:
             logger.warning('The specified num_qubits and '
                            'the provided custom circuit do not match.')
         self._circuit = convert_to_basis_gates(circuit)
         if state_vector is not None:
             self._state = None
             self._state_vector = None
             logger.warning(
                 'The provided state_vector is ignored in favor of '
                 'the provided custom circuit.')
     else:
         if state_vector is None:
             if self._state == 'zero':
                 self._state_vector = np.array([1.0] + [0.0] * (size - 1))
             elif self._state == 'uniform':
                 self._state_vector = np.array([1.0 / np.sqrt(size)] * size)
             elif self._state == 'random':
                 self._state_vector = normalize_vector(
                     aqua_globals.random.random(size))
             else:
                 raise AquaError('Unknown state {}'.format(self._state))
         else:
             if len(state_vector) != np.power(2, self._num_qubits):
                 raise AquaError(
                     'The state vector length {} is incompatible with '
                     'the number of qubits {}'.format(
                         len(state_vector), self._num_qubits))
             self._state_vector = normalize_vector(state_vector)
             self._state = None
Ejemplo n.º 2
0
    def __init__(self,
                 num_qubits,
                 state="zero",
                 state_vector=None,
                 circuit=None):
        """Constructor.

        Args:
            num_qubits (int): number of qubits
            state (str): `zero`, `uniform` or `random`
            state_vector (numpy.ndarray): customized vector
            circuit (QuantumCircuit): the actual custom circuit for the desired initial state
        Raises:
            AquaError: invalid input
        """
        # pylint: disable=comparison-with-callable
        loc = locals().copy()
        # since state_vector is a numpy array of complex numbers which aren't json valid,
        # remove it from validation
        del loc['state_vector']
        self.validate(loc)
        super().__init__()
        self._num_qubits = num_qubits
        self._state = state
        size = np.power(2, self._num_qubits)
        self._circuit = None
        if circuit is not None:
            if circuit.width() != num_qubits:
                logger.warning('The specified num_qubits and '
                               'the provided custom circuit do not match.')
            self._circuit = convert_to_basis_gates(circuit)
            if state_vector is not None:
                self._state = None
                self._state_vector = None
                logger.warning(
                    'The provided state_vector is ignored in favor of '
                    'the provided custom circuit.')
        else:
            if state_vector is None:
                if self._state == 'zero':
                    self._state_vector = np.array([1.0] + [0.0] * (size - 1))
                elif self._state == 'uniform':
                    self._state_vector = np.array([1.0 / np.sqrt(size)] * size)
                elif self._state == 'random':
                    self._state_vector = normalize_vector(
                        aqua_globals.random.rand(size))
                else:
                    raise AquaError('Unknown state {}'.format(self._state))
            else:
                if len(state_vector) != np.power(2, self._num_qubits):
                    raise AquaError(
                        'The state vector length {} is incompatible with '
                        'the number of qubits {}'.format(
                            len(state_vector), self._num_qubits))
                self._state_vector = normalize_vector(state_vector)
                self._state = None
Ejemplo n.º 3
0
    def __init__(self,
                 num_qubits: int,
                 state: str = 'zero',
                 state_vector: Optional[np.ndarray] = None,
                 circuit: Optional[QuantumCircuit] = None) -> None:
        """Constructor.

        Args:
            num_qubits: number of qubits, has a min. value of 1.
            state: `zero`, `uniform` or `random`
            state_vector: customized vector
            circuit: the actual custom circuit for the desired initial state
        Raises:
            AquaError: invalid input
        """
        validate_min('num_qubits', num_qubits, 1)
        validate_in_set('state', state, {'zero', 'uniform', 'random'})
        super().__init__()
        self._num_qubits = num_qubits
        self._state = state
        size = np.power(2, self._num_qubits)
        self._circuit = None
        if circuit is not None:
            if circuit.width() != num_qubits:
                logger.warning('The specified num_qubits and '
                               'the provided custom circuit do not match.')
            self._circuit = convert_to_basis_gates(circuit)
            if state_vector is not None:
                self._state = None
                self._state_vector = None
                logger.warning(
                    'The provided state_vector is ignored in favor of '
                    'the provided custom circuit.')
        else:
            if state_vector is None:
                if self._state == 'zero':
                    self._state_vector = np.array([1.0] + [0.0] * (size - 1))
                elif self._state == 'uniform':
                    self._state_vector = np.array([1.0 / np.sqrt(size)] * size)
                elif self._state == 'random':
                    self._state_vector = normalize_vector(
                        aqua_globals.random.rand(size))
                else:
                    raise AquaError('Unknown state {}'.format(self._state))
            else:
                if len(state_vector) != np.power(2, self._num_qubits):
                    raise AquaError(
                        'The state vector length {} is incompatible with '
                        'the number of qubits {}'.format(
                            len(state_vector), self._num_qubits))
                self._state_vector = normalize_vector(state_vector)
                self._state = None
Ejemplo n.º 4
0
    def __init__(self, state_vector):
        """Constructor.

        Args:
            state_vector: vector representation of the desired quantum state
        """
        if not is_power_of_2(len(state_vector)):
            raise AquaError('The length of the input state vector needs to be a power of 2.')
        self._num_qubits = log2(len(state_vector))
        self._state_vector = normalize_vector(state_vector)
    def __init__(self, state_vector):
        """Constructor.

        Args:
            state_vector (numpy.ndarray): vector representation of the desired quantum state
        Raises:
            AquaError: invalid input
        """
        warnings.warn('The StateVectorCircuit class is deprecated as of Qiskit Aqua 0.9.0 and will '
                      'be removed no earlier than 3 months after the release. If you need to '
                      'initialize a circuit, use the QuantumCircuit.initialize or '
                      'QuantumCircuit.isometry methods. For a parameterized initialization, try '
                      'the qiskit.ml.circuit.library.RawFeatureVector class.',
                      DeprecationWarning, stacklevel=2)

        if not is_power_of_2(len(state_vector)):
            raise AquaError('The length of the input state vector needs to be a power of 2.')
        self._num_qubits = log2(len(state_vector))
        self._state_vector = normalize_vector(state_vector)