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)
def test_noise_operators(init, gate_time, rate):
    """Test PRAGMA operators applying noise with PyQuEST interface"""
    op = init[0]

    (Gate_time, Rate, q0) = ('gate_time',
                             'rate', 0)
    operation = op(qubit=q0, gate_time=Gate_time, rate=Rate)
    substitution_dict = {'gate_time': gate_time, 'rate': rate}
    operation = operation.substitute_parameters(substitution_dict)
    env = utils.createQuestEnv()()
    qureg = utils.createDensityQureg()(1, env)
    cheat.initPlusState()(qureg=qureg)
    init[2](qureg=qureg, probability=operation.probability().float(), qubit=0)
    density_matrix_ref = cheat.getDensityMatrix()(qureg)
    test_dict = {'ro': list()}

    cheat.initPlusState()(qureg)
    pyquest_call_operation(operation=operation.substitute_parameters(substitution_dict),
                           qureg=qureg,
                           classical_bit_registers=dict(),
                           classical_float_registers=dict(),
                           classical_complex_registers=test_dict,
                           output_bit_register_dict=dict(),
                           )

    density_matrix_test = cheat.getDensityMatrix()(qureg)

    npt.assert_array_almost_equal(density_matrix_ref, density_matrix_test)

    utils.destroyQureg().call_interactive(qureg, env)
    utils.destroyQuestEnv().call_interactive(env)

    operation = operation.substitute_parameters(substitution_dict)

    env = utils.createQuestEnv()()
    qureg = utils.createDensityQureg()(1, env)
    cheat.initPlusState()(qureg=qureg)
    init[2](qureg=qureg, probability=operation.probability().float(), qubit=0)
    density_matrix_ref = cheat.getDensityMatrix()(qureg)

    cheat.initPlusState()(qureg)
    pyquest_call_operation(operation=operation, qureg=qureg, classical_bit_registers=dict(),
                           classical_float_registers=dict(),
                           classical_complex_registers=test_dict,
                           output_bit_register_dict=dict(),)

    density_matrix_test = cheat.getDensityMatrix()(qureg)

    npt.assert_array_almost_equal(density_matrix_ref, density_matrix_test)

    utils.destroyQureg().call_interactive(qureg, env)
    utils.destroyQuestEnv().call_interactive(env)
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_qureg():
    """Test set qureg PRAGMA with PyQuEST interface"""
    env = utils.createQuestEnv()()
    qureg_density = utils.createDensityQureg()(2, env)
    qureg_wave = utils.createQureg()(2, env)
    cheat.initBlankState()(qureg=qureg_density)
    cheat.initBlankState()(qureg=qureg_wave)

    density_start = cheat.getDensityMatrix()(qureg_density)
    wave_start = cheat.getStateVector()(qureg_wave)

    set_wave = np.array([2, 1, 0, 1], dtype=complex)
    set_density = np.array([[1, 0, 2, 0],
                            [0, 2, 3, 3],
                            [3, 1, 0, 0],
                            [0, 1, 2, 0]], dtype=complex)

    pyquest_call_operation(operation=ops.PragmaSetStateVector(set_wave),
                           qureg=qureg_wave,
                           classical_bit_registers=dict(),
                           classical_float_registers=dict(),
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    pyquest_call_operation(operation=ops.PragmaSetDensityMatrix(set_density),
                           qureg=qureg_density,
                           classical_bit_registers=dict(),
                           classical_float_registers=dict(),
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)

    npt.assert_array_equal(density_start, [[0] * 4] * 4)
    npt.assert_array_equal(wave_start, [0] * 4)
    npt.assert_array_equal(set_wave, cheat.getStateVector()(qureg_wave))
    npt.assert_array_equal(set_density, cheat.getDensityMatrix()(qureg_density))

    utils.destroyQureg().call_interactive(qureg_wave, env)
    utils.destroyQureg().call_interactive(qureg_density, env)
    utils.destroyQuestEnv().call_interactive(env)
def _execute_GetStateVector(
    operation: Union[ops.PragmaGetStateVector, ops.PragmaGetDensityMatrix],
    qureg: tqureg,
    classical_complex_registers: Dict[str, List[complex]],
) -> None:
    tags = operation.tags()
    if 'PragmaGetStateVector' in tags:
        is_state = True
        quest_obj = qcheat.getStateVector()
    if 'PragmaGetDensityMatrix' in tags:
        is_state = False
        quest_obj = qcheat.getDensityMatrix()

    if operation.circuit() is None:
        if is_state is True:
            statevector = quest_obj(qureg=qureg, )
        else:
            density_matrix = quest_obj(qureg=qureg, )
    else:
        N = qureg.numQubitsRepresented
        env = qutils.createQuestEnv()()
        if qureg.isDensityMatrix:
            workspace = qutils.createDensityQureg()(N, env)
        else:
            workspace = qutils.createQureg()(N, env)
        qutils.cloneQureg()(workspace, qureg)
        if operation.circuit() is not None:
            pyquest_call_circuit(
                circuit=operation.circuit(),
                qureg=workspace,
                classical_bit_registers=dict(),
                classical_float_registers=dict(),
                classical_complex_registers=dict(),
                output_bit_register_dict=dict(),
            )
        if is_state is True:
            statevector = quest_obj(qureg=workspace)
        else:
            density_matrix = quest_obj(qureg=workspace).flatten()

    classical_complex_registers[operation.readout(
    )] = statevector if is_state is True else density_matrix
Example #6
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)
def test_get_complicated() -> None:
    """Test less simple get functions

    getDensityMatrix, getStateVector, getOccupationProbability, getRepeatedMeasurement
    """
    env = utils.createQuestEnv()()
    qureg_statevec = utils.createQureg()(2, env)
    qureg_dens = utils.createDensityQureg()(2, env)
    cheat.initZeroState()(qureg_statevec)
    cheat.initZeroState()(qureg_dens)

    dens_mat = cheat.getDensityMatrix()(qureg=qureg_dens)
    state_vec = cheat.getStateVector()(qureg=qureg_statevec)

    occupation_proba_dens = cheat.getOccupationProbability()(qureg=qureg_dens)
    occupation_proba_statevec = cheat.getOccupationProbability()(
        qureg=qureg_statevec)

    repeated_meas_dens = cheat.getRepeatedMeasurement()(
        qureg=qureg_dens,
        number_measurements=5,
        qubits_to_readout_index_dict={
            0: 0,
            1: 1
        })
    repeated_meas_statevec = cheat.getRepeatedMeasurement()(
        qureg=qureg_statevec,
        number_measurements=5,
        qubits_to_readout_index_dict={
            0: 0,
            1: 1
        })

    assert np.all(dens_mat == np.array([[1, 0, 0, 0], [0, 0, 0, 0],
                                        [0, 0, 0, 0], [0, 0, 0, 0]]))
    assert np.all(state_vec == np.array([1, 0, 0, 0]))
    assert np.all(occupation_proba_dens == np.array([1, 0, 0, 0]))
    assert np.all(occupation_proba_statevec == np.array([1, 0, 0, 0]))
    assert np.all(repeated_meas_dens == np.array([[0., 0.]] * 5))
    assert np.all(repeated_meas_statevec == np.array([[0., 0.]] * 5))
Example #8
0
def test_QuReg() -> None:
    """Testing the creation, cloning, destruction and reporting of a QuReg"""
    env = utils.createQuestEnv()()
    wave_qureg = utils.createQureg()(num_qubits=2, env=env)
    cheat.initZeroState()(qureg=wave_qureg)
    density_qureg = utils.createDensityQureg()(num_qubits=2, env=env)
    cheat.initZeroState()(qureg=density_qureg)
    cloned_qureg = utils.createCloneQureg()(qureg=density_qureg, env=env)
    cheat.initZeroState()(qureg=cloned_qureg)

    try:
        [wave_string, density_string, cloned_string] = ['', '', '']
        cheat.getEnvironmentString()(env=env,
                                     qureg=wave_qureg,
                                     string=wave_string)
        cheat.getEnvironmentString()(env=env,
                                     qureg=density_qureg,
                                     string=density_string)
        cheat.getEnvironmentString()(env=env,
                                     qureg=cloned_qureg,
                                     string=cloned_string)
        assert cloned_string == density_string
        cheat.getEnvironmentString()(env=env,
                                     qureg=cloned_qureg,
                                     string=cloned_string)
        assert cloned_string == wave_string
    except NotImplementedError:
        pass  # getEnvironmentString unittest not implemented

    assert wave_qureg.isDensityMatrix == False
    assert density_qureg.isDensityMatrix == True
    assert cloned_qureg.isDensityMatrix == True
    assert np.all(cheat.getDensityMatrix()(
        qureg=density_qureg) == cheat.getDensityMatrix()(qureg=cloned_qureg))
    assert not np.all(cheat.getStateVector()(
        qureg=wave_qureg) == cheat.getDensityMatrix()(qureg=cloned_qureg))

    npt.assert_raises(TypeError, utils.cloneQureg(), cloned_qureg, wave_qureg)

    to_be_cloned = utils.createDensityQureg()(num_qubits=3, env=env)
    cheat.initZeroState()(qureg=to_be_cloned)
    clone_into = utils.createDensityQureg()(num_qubits=3, env=env)
    cheat.initZeroState()(qureg=clone_into)
    utils.cloneQureg()(clone_into, to_be_cloned)
    assert clone_into.isDensityMatrix == True

    result_type_list = ['', '', '']
    argument_type_list = [[''], [''], ['']]
    for qureg in [wave_qureg, density_qureg, cloned_qureg]:
        utils.reportQuregParams()(qureg=qureg)
        utils.reportState()(qureg=qureg)
        utils.reportStateToScreen()(qureg=qureg, env=env)
        result_type_list[0] = utils.reportQuregParams().restype
        argument_type_list[0] = utils.reportQuregParams().argtype
        result_type_list[1] = utils.reportState().restype
        argument_type_list[1] = utils.reportState().argtype
        result_type_list[2] = utils.reportStateToScreen().restype
        argument_type_list[2] = utils.reportStateToScreen().argtype

    npt.assert_array_equal(result_type_list, ["void", "void", "void"])
    npt.assert_equal(argument_type_list,
                     [["Qureg"], ["Qureg"], ["Qureg", "QuESTEnv", "int"]])

    for qureg in [wave_qureg, density_qureg, cloned_qureg]:
        utils.destroyQureg()(env=env, qubits=qureg)