def _execute_GetOccupationProbability(
        operation: Union[
            ops.PragmaGetOccupationProbability],
        qureg: tqureg,
        classical_float_registers: Dict[str, List[float]],
) -> None:
    tags = operation.tags()
    quest_obj = qcheat.getOccupationProbability()
    if 'PragmaGetRotatedOccupationProbability' in tags:
        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(),)
        occ_probabilities = np.real(quest_obj(qureg=workspace))
        qutils.destroyQureg()(qubits=workspace, env=env)
        del workspace
        qutils.destroyQuestEnv()(env)
        del env
    else:
        occ_probabilities = np.real(quest_obj(qureg=qureg, ))

    classical_float_registers[operation.readout()] = occ_probabilities
def _execute_GetPauliProduct(
    operation: ops.PragmaGetPauliProduct,
    qureg: tqureg,
    classical_float_registers: Dict[str, List[float]],
) -> None:

    if operation.qubit_paulis == {}:
        classical_float_registers[operation.readout()] = [
            1.0,
        ]
        return None
    N = qureg.numQubitsRepresented
    env = qutils.createQuestEnv()()
    if qureg.isDensityMatrix:
        workspace = qutils.createDensityQureg()(N, env)
        workspace_pp = qutils.createDensityQureg()(N, env)
    else:
        workspace = qutils.createQureg()(N, env)
        workspace_pp = 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(),
        )
    qubits = list()
    paulis = list()
    for qubit, pauli in operation.qubit_paulis().items():
        qubits.append(qubit)
        paulis.append(pauli)
    pp = qcheat.calcExpecPauliProd().call_interactive(qureg=workspace,
                                                      qubits=qubits,
                                                      paulis=paulis,
                                                      workspace=workspace_pp)
    qutils.destroyQureg()(qubits=workspace_pp, env=env)
    qutils.destroyQureg()(qubits=workspace, env=env)
    del workspace
    qutils.destroyQuestEnv()(env)
    del env
    classical_float_registers[operation.readout()] = [
        pp,
    ]
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 #4
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)