ops.PragmaRandomNoise(0, 0.005, 0.02, 0.01),
    ops.PragmaGeneralNoise(
        0, 0.005, np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)),
    ops.PragmaConditional('ro', 0, Circuit()),
    ops.PragmaRepeatGate(3),
    ops.PragmaBoostNoise(0.004),
    ops.PragmaStopParallelBlock([0, 1], 0.002),
    ops.PragmaSleep([0, 1], 0.002),
    ops.PragmaActiveReset(0),
    ops.PragmaGlobalPhase(0.03),
    ops.MeasureQubit(0, 'ro', 0),
    ops.PragmaRepeatedMeasurement('ro', 20, {}),
    ops.PragmaGetStateVector('ro', Circuit()),
    ops.PragmaGetDensityMatrix('ro', Circuit()),
    ops.PragmaGetOccupationProbability('ro', Circuit()),
    ops.PragmaGetPauliProduct({}, 'ro', Circuit()),
    ops.PragmaStartDecompositionBlock([0, 1], {}),
    ops.PragmaStopDecompositionBlock([0, 1]),
    ops.InputSymbolic('other', 0),
])
def test_all_operations(op: Any):
    """Test all operations with mocked interface"""
    circuit = Circuit()
    circuit += ops.DefinitionFloat(name='ro', length=1, is_output=False)
    circuit += ops.DefinitionBit(name='ro', length=1, is_output=False)
    circuit += ops.DefinitionComplex(name='ro', length=1, is_output=False)
    circuit += ops.DefinitionUsize(name='ro', length=1, is_output=False)
    circuit += op
    mocked_call_circuit(circuit=circuit,
                        classical_bit_registers={},
                        classical_float_registers={},
Example #2
0
import pytest
import sys
import numpy as np
import numpy.testing as npt
from qoqo import operations as ops
from qoqo import Circuit
from typing import List


@pytest.mark.parametrize('measurement', [
    (ops.MeasureQubit(qubit=0, readout='ro', readout_index=0), int, 0),
    (ops.PragmaRepeatedMeasurement(readout='ro',
                                   number_measurements=10), int, 0),
    (ops.PragmaGetPauliProduct(
        qubit_paulis={
            0: 1,
            1: 2
        }, readout='ro', circuit=Circuit()), float, 1),
    (ops.PragmaGetOccupationProbability(readout='ro',
                                        circuit=Circuit()), float, 1),
    (ops.PragmaGetStateVector(readout='ro', circuit=Circuit()), complex, 2),
    (ops.PragmaGetDensityMatrix(readout='ro', circuit=Circuit()), complex, 2),
])
def test_circuit(measurement):
    """Test building a circuit"""
    circuit = Circuit()
    circuit += ops.DefinitionFloat(name='ro', length=1, is_output=True)
    circuit += ops.DefinitionComplex(name='ro', length=1, is_output=True)
    circuit += ops.DefinitionBit(name='ro', length=1, is_output=True)
    circuit += ops.PauliX(qubit=0)
    circuit += measurement[0]
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)