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_random_noise_operator(gate_time, depolarisation_rate, dephasing_rate):
    """Test PRAGMA operators applying random noise (stochastic unravelling) with PyQuEST interface"""
    op = ops.PragmaRandomNoise
    test_dict = {'ro': list()}

    (Gate_time, depol_Rate, dephasing_Rate) = ('gate_time',
                                               'depolarisation_rate',
                                               'dephasing_rate',
                                               )
    operation = op(qubit=0, gate_time=Gate_time,
                   depolarising_rate=depol_Rate, dephasing_rate=dephasing_Rate)

    substitution_dict = {'gate_time': gate_time,
                         'depolarisation_rate': depolarisation_rate,
                         'dephasing_rate': dephasing_rate}
    env = utils.createQuestEnv()()
    qureg = utils.createQureg()(1, env)
    state_vec_ref = cheat.getStateVector()(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(),
                           )
    if gate_time == 0 or (dephasing_rate == 0 and depolarisation_rate == 0):
        state_vec = cheat.getStateVector()(qureg)
        npt.assert_array_almost_equal(state_vec_ref, state_vec)

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

    operation.substitute_parameters(substitution_dict)

    env = utils.createQuestEnv()()
    qureg = utils.createQureg()(1, env)
    state_vec_ref = cheat.getStateVector()(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(),)
    if gate_time == 0 or (dephasing_rate == 0 and depolarisation_rate == 0):
        state_vec = cheat.getStateVector()(qureg)
        npt.assert_array_almost_equal(state_vec_ref, state_vec)

    utils.destroyQureg().call_interactive(qureg, env)
    utils.destroyQuestEnv().call_interactive(env)
def test_measurement():
    """Test gate operations without free parameters with PyQuEST interface"""
    op = ops.MeasureQubit(qubit=0, readout='test', readout_index=0)
    test_dict = {'test': np.array([0, 0, 0, 0])}
    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(2, env)
    state = np.array([0, 1, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(op, qubits,
                           classical_bit_registers=test_dict,
                           classical_float_registers=dict(),
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    utils.destroyQureg()(qubits, env=env)
    utils.destroyQuestEnv()(env)
    assert test_dict['test'][0]
def test_pragma_repeated_measurement_pyquest() -> None:
    """Test repeated measurement with PyQuEST interface"""
    op = ops.PragmaRepeatedMeasurement
    operation = op(readout='ro',
                   qubit_mapping={0: 0},
                   number_measurements=100)
    test_dict: Dict[str, List[List[bool]]] = {'ro': list(list())}
    test_dict2: Dict[str, List[bool]] = {'ro': list()}
    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(2, env)
    state = 1 / np.sqrt(2) * np.array([1, 1, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=test_dict2,
                           classical_float_registers=dict(),
                           classical_complex_registers=dict(),
                           output_bit_register_dict=test_dict,)
    utils.destroyQureg()(qubits, env=env)
    utils.destroyQuestEnv()(env)
    assert len(test_dict['ro']) == 100
def test_pragma_get_occupation_probability_pyquest(init) -> None:
    """Test get occupation probability with PyQuEST interface"""
    op = ops.PragmaGetOccupationProbability
    test_dict: Dict[str, List[float]] = {'ro': [0, 0]}
    operation = op(readout='ro',
                    circuit=Circuit()
                   )
    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(2, env)
    state = np.array([0, 1, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))

    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    utils.destroyQureg()(qubits=qubits, env=env)
    utils.destroyQuestEnv()(env)
    npt.assert_array_almost_equal(test_dict['ro'], init[1])
def test_pragma_get_statevec_pyquest(init) -> None:
    """Test get state vector with PyQuEST interface"""
    op = ops.PragmaGetStateVector
    test_dict: Dict[str, List[complex]] = {'ro': [0, 0, 0, 0]}
    operation = op(readout='ro',
                   circuit=Circuit(),
                   )
    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(2, env)
    state = 1 / np.sqrt(2) * np.array([1, 1, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))

    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=dict(),
                           classical_complex_registers=test_dict,
                           output_bit_register_dict=dict(),)
    utils.destroyQureg()(qubits=qubits, env=env)
    utils.destroyQuestEnv()(env)
    npt.assert_array_almost_equal(test_dict['ro'], init[1], decimal=4)
def test_pragma_get_densitymatrix_pyquest() -> None:
    """Test get density matrix with PyQuEST interface"""
    op = ops.PragmaGetDensityMatrix
    test_dict: Dict[str, List[complex]] = {'ro': [0, 0, 0, 0]}
    operation = op(readout='ro',
                   circuit=Circuit()
                   )
    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(1, env)
    state = 1 / np.sqrt(2) * np.array([1, 1])
    density_matrix = 1 / 2 * np.array([[1, 1], [1, 1]]).flatten()
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))

    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=dict(),
                           classical_complex_registers=test_dict,
                           output_bit_register_dict=dict(),)
    utils.destroyQureg()(qubits=qubits, env=env)
    utils.destroyQuestEnv()(env)
    npt.assert_array_almost_equal(test_dict['ro'], density_matrix)
def test_pragma_get_pauli_prod_measurement_pyquest(init) -> None:
    """Test measuring product of pauli operators with PyQuEST interface"""
    test_dict: Dict[str, List[float]] = {'ro': [0, 0, 0, 0]}

    op = ops.PragmaGetPauliProduct

    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(2, env)
    state = init[0]
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    operation = op(readout='ro',
                   qubit_paulis=init[2],
                   circuit=Circuit())

    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    npt.assert_array_almost_equal(test_dict['ro'], init[1])
    utils.destroyQureg()(qubits, env=env)
    utils.destroyQuestEnv()(env)
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 test_conditional_pragma():
    """Test PRAGMA conditional operation"""
    test_dict = {'ro': [False, False],
                 'cond': [True, False]}

    circuit0 = Circuit()
    circuit0 += ops.PauliX(qubit=0)
    circuit0 += ops.MeasureQubit(readout='ro', qubit=0, readout_index=0)

    circuit1 = Circuit()
    circuit1 += ops.PauliX(qubit=1)
    circuit1 += ops.MeasureQubit(readout='ro', qubit=1, readout_index=1)

    operation = ops.PragmaConditional(
        condition_register='cond',
        condition_index=0,
        circuit=circuit0)

    env = utils.createQuestEnv()()
    qureg = utils.createQureg()(2, env)
    test_dict = {'ro': [False, False],
                 'cond': [True, False]}
    pyquest_call_operation(operation=operation,
                           qureg=qureg,
                           classical_bit_registers=test_dict,
                           classical_float_registers=dict(),
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),
                           )

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

    assert test_dict['ro'] == [True, False]

    operation = ops.PragmaConditional(
        condition_register='cond',
        condition_index=0,
        circuit=circuit1)

    env = utils.createQuestEnv()()
    qureg = utils.createQureg()(2, env)
    test_dict = {'ro': [False, False],
                 'cond': [True, False]}
    pyquest_call_operation(operation=operation,
                           qureg=qureg,
                           classical_bit_registers=test_dict,
                           classical_float_registers=dict(),
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),
                           )

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

    assert test_dict['ro'] == [False, True]

    operation = ops.PragmaConditional(
        condition_register='not_in',
        condition_index=0,
        circuit=circuit1)


    env = utils.createQuestEnv()()
    qureg = utils.createQureg()(2, env)
    with pytest.raises(RuntimeError):
        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(),
                               )

    utils.destroyQureg().call_interactive(qureg, env)
    utils.destroyQuestEnv().call_interactive(env)
def test_pragma_get_pauli_product_pyquest() -> None:
    """Test repeated measurement with PyQuEST interface"""

    operation = ops.PragmaGetPauliProduct(readout='ro',
                                          qubit_paulis={0: 3},
                                          circuit=Circuit()
                                          )
    test_dict: Dict[str, List[float]] = {'ro': [0.0]}
    env = utils.createQuestEnv()()
    qubits = utils.createQureg()(2, env)
    state = np.array([1, 0, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    assert test_dict['ro'][0] == 1

    test_dict = {'ro': list()}
    state = np.array([0, 1, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)

    assert test_dict['ro'][0] == -1

    test_dict = {'ro': list()}
    state = 1 / np.sqrt(2) * np.array([1, 1, 0, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    assert test_dict['ro'][0] == 0

    operation = ops.PragmaGetPauliProduct(readout='ro',
                                          qubit_paulis={0: 3, 1: 3},
                                          circuit=Circuit()
                                          )
    test_dict = {'ro': list()}
    state = 1 / np.sqrt(2) * np.array([0, 1, 1, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    assert np.isclose(test_dict['ro'][0], -1)

    test_dict = {'ro': list()}
    state = 1 / np.sqrt(2) * np.array([0, 1, -1, 0])
    cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state))
    pyquest_call_operation(operation=operation, qureg=qubits, classical_bit_registers=dict(),
                           classical_float_registers=test_dict,
                           classical_complex_registers=dict(),
                           output_bit_register_dict=dict(),)
    assert np.isclose(test_dict['ro'][0], -1)

    utils.destroyQureg()(qubits=qubits, env=env)
    utils.destroyQuestEnv()(env)