Ejemplo n.º 1
0
    def construct_circuit(self, x, qr=None, inverse=False):
        """
        Construct the second order expansion based on given data.

        Args:
            x (numpy.ndarray): 1-D to-be-encoded data.
            qr (QauntumRegister): the QuantumRegister object for the circuit, if None,
                                  generate new registers with name q.
            inverse (bool): inverse
        Returns:
            QuantumCircuit: a quantum circuit transform data x.
        Raises:
            TypeError: invalid input
            ValueError: invalid input
        """
        if not isinstance(x, np.ndarray):
            raise TypeError("x must be numpy array.")
        if x.ndim != 1:
            raise ValueError("x must be 1-D array.")
        if x.shape[0] != self._feature_dimension:
            raise ValueError("Unexpected feature vector dimension.")

        state_vector = np.pad(x, (0, (1 << self.num_qubits) - len(x)),
                              'constant')

        svc = StateVectorCircuit(state_vector)
        return svc.construct_circuit(register=qr)
Ejemplo n.º 2
0
    def construct_circuit(self, mode, register=None):
        """
        Construct the statevector of desired initial state.

        Args:
            mode (string): `vector` or `circuit`. The `vector` mode produces the vector.
                            While the `circuit` constructs the quantum circuit corresponding that
                            vector.
            register (QuantumRegister): register for circuit construction.

        Returns:
            QuantumCircuit or numpy.ndarray: statevector.

        Raises:
            AquaError: when mode is not 'vector' or 'circuit'.
        """
        if mode == 'vector':
            if self._state_vector is None:
                if self._circuit is not None:
                    self._state_vector = np.asarray(
                        q_execute(
                            self._circuit,
                            BasicAer.get_backend('statevector_simulator')).
                        result().get_statevector(self._circuit))
            return self._state_vector
        elif mode == 'circuit':
            if self._circuit is None:
                if register is None:
                    register = QuantumRegister(self._num_qubits, name='q')

                # create emtpy quantum circuit
                circuit = QuantumCircuit()

                # if register is actually a list of qubits
                if type(register) is list:

                    # loop over all qubits and add the required registers
                    for q in register:
                        if not circuit.has_register(q[0]):
                            circuit.add_register(q[0])
                else:
                    # if an actual register is given, add it
                    circuit.add_register(register)

                if self._state is None or self._state == 'random':
                    svc = StateVectorCircuit(self._state_vector)
                    svc.construct_circuit(circuit, register)
                elif self._state == 'zero':
                    pass
                elif self._state == 'uniform':
                    for i in range(self._num_qubits):
                        circuit.u2(0.0, np.pi, register[i])
                else:
                    pass
                self._circuit = circuit
            return self._circuit.copy()
        else:
            raise AquaError('Mode should be either "vector" or "circuit"')
Ejemplo n.º 3
0
    def construct_circuit(self, mode='circuit', register=None):
        # pylint: disable=import-outside-toplevel
        from qiskit import BasicAer

        if mode == 'vector':
            if self._state_vector is None:
                if self._circuit is not None:
                    self._state_vector = np.asarray(
                        q_execute(
                            self._circuit,
                            BasicAer.get_backend('statevector_simulator')).
                        result().get_statevector(self._circuit))
            return self._state_vector
        elif mode == 'circuit':
            if self._circuit is None:
                # create empty quantum circuit
                circuit = QuantumCircuit()

                if register is None:
                    register = QuantumRegister(self._num_qubits, name='q')

                if isinstance(register, QuantumRegister):
                    circuit.add_register(register)
                elif isinstance(register, list):
                    for q in register:
                        if isinstance(q, Qubit):
                            if not circuit.has_register(q.register):
                                circuit.add_register(q.register)
                        else:
                            raise AquaError('Unexpected qubit type {}.'.format(
                                type(q)))
                else:
                    raise AquaError('Unexpected register type {}.'.format(
                        type(register)))

                if self._state is None or self._state == 'random':
                    svc = StateVectorCircuit(self._state_vector)
                    svc.construct_circuit(circuit=circuit, register=register)
                elif self._state == 'uniform':
                    for i in range(self._num_qubits):
                        circuit.u(np.pi / 2, 0.0, np.pi, register[i])
                elif self._state == 'zero':
                    pass
                else:
                    AquaError('Unexpected state mode {}.'.format(self._state))
                self._circuit = circuit
            return self._circuit.copy()
        else:
            raise AquaError('Mode should be either "vector" or "circuit"')
Ejemplo n.º 4
0
 def reset_qc(self, statevector):
     st = StateVectorCircuit(statevector)
     self.qc = st.construct_circuit(register=self.q)
Ejemplo n.º 5
0
 def reset_qc(self, statevector):
     st = StateVectorCircuit(statevector)
     self.qc = st.construct_circuit()
     self.q = self.qc.qubits[0]
     self.a = self.qc.qubits[1]
     self.b = self.qc.qubits[2]