Beispiel #1
0
    def call_interactive(self, qureg_clone: tqureg, qureg_original: tqureg) -> None:
        r"""Interactive call of PyQuest-cffi

        Args:
            qureg_original: Qureg to be cloned
            qureg_clone: Cloned qureg

        Raises:
            TypeError: The quregs need to be of the same type, so either both density
                matrices OR both wave functions
            ValueError: The quregs need to contain the same number of qubits
        """
        if qureg_clone.isDensityMatrix and qureg_original.isDensityMatrix:
            if cheat.getNumQubits()(qureg=qureg_clone) == cheat.getNumQubits()(qureg=qureg_clone):
                quest.cloneQureg(qureg_clone, qureg_original)
            else:
                raise ValueError("The quregs need to contain the same number of qubits")
        elif not qureg_clone.isDensityMatrix and not qureg_original.isDensityMatrix:
            if cheat.getNumQubits()(qureg=qureg_clone) == cheat.getNumQubits()(qureg=qureg_clone):
                quest.cloneQureg(qureg_clone, qureg_original)
            else:
                raise ValueError("The quregs need to contain the same number of qubits")
        else:
            raise TypeError("The quregs need to be of the same type, so either both "
                            + "density matrices OR both wave functions")
def test_get_simple() -> None:
    """Testing simple get functions.

    getStateVectoratIndex/getAmp, getDensityMatrixatRowColumn/getDensityAmp,
    getAbsoluteValSquaredatIndex/getProbAmp, getRealAmp, getImagAmp
    """
    env = utils.createQuestEnv()()
    qureg = utils.createQureg()(2, env)
    cheat.initZeroState()(qureg)
    index = 1
    operator_matrix = np.array([[1, 0, 2, 0], [0, 1, 0, 2], [0, 2, 0, 1],
                                [2, 0, 1, 0]])

    with npt.assert_raises(RuntimeError):
        density_matrix = cheat.getDensityAmp()(qureg=qureg, row=1, column=1)
    state_vec = cheat.getAmp()(qureg=qureg, index=index)
    abs_val_state_vec = cheat.getProbAmp()(qureg=qureg, index=1)
    real_val_sate_vec = cheat.getRealAmp()(qureg=qureg, index=1)
    imag_val_sate_vec = cheat.getImagAmp()(qureg=qureg, index=1)
    num_amps = cheat.getNumAmps()(qureg=qureg)
    num_qubits = cheat.getNumQubits()(qureg=qureg)
    expec_val = cheat.getExpectationValue()(qureg=qureg,
                                            operator_matrix=operator_matrix)

    assert state_vec == 0
    assert abs_val_state_vec == 0
    assert real_val_sate_vec == 0
    assert imag_val_sate_vec == 0
    assert num_amps == 4
    assert num_qubits == 2
    assert expec_val == 1

    qureg = utils.createDensityQureg()(2, env)
    cheat.initZeroState()(qureg)

    with npt.assert_raises(RuntimeError):
        state_vec = cheat.getAmp()(qureg=qureg, index=index)
        abs_val_state_vec = cheat.getProbAmp()(qureg=qureg, index=1)
        real_val_sate_vec = cheat.getRealAmp()(qureg=qureg, index=1)
        imag_val_sate_vec = cheat.getImagAmp()(qureg=qureg, index=1)
    density_matrix = cheat.getDensityAmp()(qureg=qureg, row=1, column=1)
    num_amps = cheat.getNumAmps()(qureg=qureg)
    num_qubits = cheat.getNumQubits()(qureg=qureg)
    expec_val = cheat.getExpectationValue()(qureg=qureg,
                                            operator_matrix=operator_matrix)

    assert density_matrix == 0
    assert num_amps == 4
    assert num_qubits == 2
    assert expec_val == 1
Beispiel #3
0
    def call_interactive(self,
                         qureg: tqureg,
                         pauli_hamil: paulihamil,
                         workspace: tqureg
                         ) -> float:
        r"""Interactive call of PyQuest-cffi

        Args:
            qureg: quantum register that is measured
            pauli_hamil: a PauliHamil created with createPauliHamil()
            workspace: A qureg of same type and size as input qureg, is used as temporary
                    work qureg

        Returns:
            float

        Raises:
            RuntimeError: Qureg and PauliHamil must be defined for the same number of qubits
        """
        if not (cheat.getNumQubits()(qureg=qureg) == pauli_hamil.numQubits):
            raise RuntimeError("Qureg and PauliHamil must be defined for the "
                               + "same number of qubits")
        return quest.calcExpecPauliHamil(qureg,
                                         pauli_hamil,
                                         workspace)
Beispiel #4
0
    def call_interactive(self, qureg: tqureg) -> int:
        r"""Interactive call of PyQuest-cffi

        Args:
            qureg: a qureg containing a wavefunction or a density matrix

        Returns:
            int
        """
        if qureg.isDensityMatrix:
            return 2 ** cheat.getNumQubits()(qureg=qureg)
        else:
            return quest.getNumAmps(qureg)
    def call_interactive(self, qureg: tqureg, probability: float,
                         qureg_other: tqureg) -> None:
        r"""Interactive call of PyQuest-cffi

        Args:
            qureg: quantum register to be modified
            probability: the probability of qureg_other in the modified qureg
            qureg_other: the quantum register to be mixed into qureg

        Raises:
            RuntimeError: Both quregs must be density matrix,
                but at least one of them is a wavefunction
            RuntimeError: Qureg and Qureg_other must be defined for the same number of qubits
        """
        if not qureg.isDensityMatrix or not qureg_other.isDensityMatrix:
            raise RuntimeError("Both quregs must be density matrix, " +
                               "but at least one of them is a wavefunction")
        elif not (cheat.getNumQubits()(qureg=qureg)
                  == cheat.getNumQubits()(qureg=qureg_other)):
            raise RuntimeError("Qureg and Qureg_other must be defined " +
                               "for the same number of qubits")
        else:
            quest.mixDensityMatrix(qureg, probability, qureg_other)
Beispiel #6
0
    def call_interactive(self, qureg: tqureg, reals: Union[np.ndarray,
                                                           List[float]],
                         imags: Union[np.ndarray, List[float]]) -> None:
        r"""Interactive call of PyQuest-cffi

        Args:
            qureg: the quantum register
            reals: The real parts of the statevector
            imags: The imaginary parts of the statevector

        Raises:
            RuntimeError: Size of reals and imags needs to match
            RuntimeError: Shape of reals and imags for wavefunction should be: (1, 2**qubits)
            RuntimeError: Shape of reals and imags for density matrix should be:
                (2**qubits, 2**qubits) OR (4**qubits, 1)
            RuntimeError: Shape of reals and imags for density matrix should be:
                (2**qubits, 2**qubits) OR (4**qubits, 1)
            RuntimeError: Need to set real and imaginary amplitudes for each qubit:
                2**qubits for wavefunction qureg, 4**qubits for density matrix qureg
        """
        reals = list(reals)
        imags = list(imags)
        assert len(reals) == np.max(np.shape(reals))
        assert len(imags) == np.max(np.shape(imags))
        size_amps = np.size(np.array(reals))
        if not size_amps == np.size(np.array(imags)):
            raise RuntimeError("Size of reals and imags needs to match")
        num_qubits = cheat.getNumQubits()(qureg=qureg)

        if size_amps == 2**num_qubits:
            if qureg.isDensityMatrix:
                raise RuntimeError(
                    "Shape of reals and imags for wavefunction should be: " +
                    "(1, 2**qubits)")
            pointer_reals = ffi_quest.new("{}[{}]".format(qreal, len(reals)))
            for co, c in enumerate(reals):
                pointer_reals[co] = c
            pointer_imags = ffi_quest.new("{}[{}]".format(qreal, len(imags)))
            for co, c in enumerate(imags):
                pointer_imags[co] = c
            quest.initStateFromAmps(qureg, pointer_reals, pointer_imags)
        elif size_amps == 4**num_qubits:
            if not qureg.isDensityMatrix:
                raise RuntimeError(
                    "Shape of reals and imags for density matrix should be:" +
                    "(2**qubits, 2**qubits) OR (4**qubits, 1)")
            size_amps_rows = np.size(np.array(reals), 0)
            size_amps_columns = np.size(np.array(reals), 1)
            if (not (size_amps_rows == np.size(np.array(imags), 0))
                    or not (size_amps_columns == np.size(np.array(imags), 1))):
                raise RuntimeError("Size of reals and imags needs to match")
            cheat.initZeroState()(qureg=qureg)
            if (size_amps_rows == size_amps_columns == 2**num_qubits):
                cheat.setDensityAmps()(qureg=qureg, reals=reals, imags=imags)
            elif size_amps_rows == 4**num_qubits:
                reals = np.array(reals).reshape((2**num_qubits, 2**num_qubits))
                imags = np.array(imags).reshape((2**num_qubits, 2**num_qubits))
                cheat.setDensityAmps()(qureg=qureg, reals=reals, imags=imags)
            else:
                raise RuntimeError(
                    "Shape of reals and imags should be (2**qubits, 2**qubits) OR "
                    + "(4**qubits, 1)")
        else:
            raise RuntimeError(
                "Need to set real and imaginary amplitudes for each qubit: " +
                "2**qubits for wavefunction, 4**qubits for density matrix")