def test_one_qubit_errors(prob, gate_def) -> None:
    """Testing one qubit errors and the mixDensityMatrix error"""
    op = gate_def[0]
    prob = prob * gate_def[1]
    env = utils.createQuestEnv()()
    dm = utils.createDensityQureg()(1, env)
    state = np.random.random((2, 1)) + 1j * np.random.random((2, 1))
    state = state / np.linalg.norm(state)
    state_dm = state @ state.conjugate().T
    cheat.setDensityAmps()(dm,
                           reals=np.real(state_dm),
                           imags=np.imag(state_dm))
    if gate_def[1] == 1 / 4:
        dm_other = utils.createDensityQureg()(1, env)
        op()(qureg=dm, probability=prob, qureg_other=dm_other)
    else:
        op()(qureg=dm, qubit=0, probability=prob)
    try:
        superop = op().superoperator_matrix(probability=prob)
        state_dm = state_dm.reshape((4, 1))
        end_matrix = (superop @ state_dm).reshape((2, 2), order='F')
        matrix = cheat.getDensityMatrix()(dm)
        npt.assert_array_almost_equal(matrix, end_matrix.T)
    except NotImplementedError:
        pass
def test_set_amps_qureg() -> None:
    """Testing set functions

    setAmps, setDensityAmps, setWeightedQureg
    """
    env = utils.createQuestEnv()()
    qureg_statevec = utils.createQureg()(2, env)
    qureg_dens = utils.createDensityQureg()(2, env)
    cheat.initZeroState()(qureg_statevec)
    cheat.initZeroState()(qureg_dens)

    cheat.setAmps()(qureg=qureg_statevec,
                    startind=0,
                    reals=[1, 2, 0, 1],
                    imags=[0, 3, 4, 3],
                    numamps=4)
    state_vec = cheat.getStateVector()(qureg=qureg_statevec)
    state_array = np.array([1 + 0j, 2 + 3j, 0 + 4j, 1 + 3j])
    assert np.all(state_vec == state_array)

    cheat.setDensityAmps()(
        qureg=qureg_dens,
        reals=[[1, 2, 1, 2], [0, 1, 0, 1], [1, 0, 1, 0], [2, 1, 2, 1]],
        imags=[[4, 3, 4, 3], [3, 2, 3, 2], [2, 3, 2, 3], [3, 4, 3, 4]],
    )
    dens_mat = cheat.getDensityMatrix()(qureg=qureg_dens)
    dens_array = np.array([[1 + 4j, 2 + 3j, 1 + 4j, 2 + 3j],
                           [3j, 1 + 2j, 3j, 1 + 2j], [1 + 2j, 3j, 1 + 2j, 3j],
                           [2 + 3j, 1 + 4j, 2 + 3j, 1 + 4j]])
    assert np.all(dens_mat == dens_array)

    # for a wavefunction qureg:
    quregout = utils.createQureg()(2, env)
    cheat.setWeightedQureg()(fac1=2.5,
                             qureg1=qureg_statevec,
                             fac2=3.5j,
                             qureg2=qureg_statevec,
                             facout=1,
                             quregout=quregout)
    state_vec_combined = cheat.getStateVector()(qureg=quregout)
    comparison_array = 2.5 * state_array + 3.5j * state_array + 1 * np.array(
        [1, 0, 0, 0])
    assert np.all(state_vec_combined == comparison_array)

    # for a density matrix qureg:
    quregout = utils.createDensityQureg()(2, env)
    cheat.setWeightedQureg()(fac1=2.5,
                             qureg1=qureg_dens,
                             fac2=3.5j,
                             qureg2=qureg_dens,
                             facout=0.5,
                             quregout=quregout)
    dens_mat_combined = cheat.getDensityMatrix()(qureg=quregout)
    comparison_array = 2.5 * dens_array + 3.5j * dens_array + 0.5 * np.array(
        [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
    assert np.all(dens_mat_combined == comparison_array)
Beispiel #3
0
def test_two_qubit_errors(prob, gate_def) -> None:
    """Testing two qubit errors"""
    op = gate_def[0]
    prob = prob * gate_def[1]
    env = utils.createQuestEnv()()
    dm = utils.createDensityQureg()(2, env)
    state = np.random.random((4, 1)) + 1j * np.random.random((4, 1))
    state = state / np.linalg.norm(state)
    state_dm = state @ state.conjugate().T
    state_dm = state_dm.reshape((16, 1))
    cheat.setDensityAmps()(dm,
                           startind=0,
                           reals=np.real(state_dm),
                           imags=np.imag(state_dm),
                           numamps=16)

    op()(qureg=dm, qubit1=0, qubit2=1, probability=prob)
Beispiel #4
0
def test_one_qubit_errors(prob, gate_def) -> None:
    """Testing one qubit errors"""
    op = gate_def[0]
    prob = prob * gate_def[1]
    env = utils.createQuestEnv()()
    dm = utils.createDensityQureg()(1, env)
    state = np.random.random((2, 1)) + 1j * np.random.random((2, 1))
    state = state / np.linalg.norm(state)
    state_dm = state @ state.conjugate().T
    state_dm = state_dm.reshape((4, 1))
    cheat.setDensityAmps()(dm,
                           startind=0,
                           reals=np.real(state_dm),
                           imags=np.imag(state_dm),
                           numamps=4)

    op()(qureg=dm, qubit=0, probability=prob)
    superop = op().superoperator_matrix(probability=prob)
    end_matrix = (superop @ state_dm).reshape((2, 2), order='F')
    matrix = cheat.getDensityMatrix()(dm)
    npt.assert_array_almost_equal(matrix, end_matrix)
Beispiel #5
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")