Ejemplo n.º 1
0
def test_ionq_no_midcircuit_measurement(monkeypatch, mapper_factory):
    """Test that attempts to measure mid-circuit raise exceptions."""
    def mock_send(*args, **kwargs):
        return {
            'nq': 1,
            'shots': 10,
            'output_probs': {
                '0': 0.4,
                '1': 0.6
            },
        }

    monkeypatch.setattr(_ionq_http_client, "send", mock_send)

    # Create a backend to use with an engine.
    backend = _ionq.IonQBackend(verbose=True, num_runs=10)
    eng = MainEngine(
        backend=backend,
        engine_list=[mapper_factory()],
        verbose=True,
    )
    qubit = eng.allocate_qubit()
    X | qubit
    Measure | qubit
    with pytest.raises(MidCircuitMeasurementError):
        X | qubit

    # atexit sends another FlushGate, therefore we remove the backend:
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.active_qubits = []
    eng.next_engine = dummy
Ejemplo n.º 2
0
def test_forwarder_engine():
    backend = DummyEngine(save_commands=True)
    engine0 = DummyEngine()
    main_engine = MainEngine(backend=backend,
                             engine_list=[engine0])

    def cmd_mod_fun(cmd):
        cmd.tags = "NewTag"
        return cmd

    forwarder_eng = _basics.ForwarderEngine(backend, cmd_mod_fun)
    engine0.next_engine = forwarder_eng
    forwarder_eng2 = _basics.ForwarderEngine(engine0)
    main_engine.next_engine = forwarder_eng2
    qubit = main_engine.allocate_qubit()
    H | qubit
    # Test if H gate was sent through forwarder_eng and tag was added
    received_commands = []
    # Remove Allocate and Deallocate gates
    for cmd in backend.received_commands:
        if not (isinstance(cmd.gate, FastForwardingGate) or
                isinstance(cmd.gate, ClassicalInstructionGate)):
            received_commands.append(cmd)
    for cmd in received_commands:
        print(cmd)
    assert len(received_commands) == 1
    assert received_commands[0].gate == H
    assert received_commands[0].tags == "NewTag"
Ejemplo n.º 3
0
def test_ionq_sent_error(monkeypatch, mapper_factory):
    """Test that errors on "send" will raise back out."""
    # patch send
    type_error = TypeError()
    mock_send = mock.MagicMock(side_effect=type_error)
    monkeypatch.setattr(_ionq_http_client, "send", mock_send)

    backend = _ionq.IonQBackend()
    eng = MainEngine(
        backend=backend,
        engine_list=[mapper_factory()],
        verbose=True,
    )
    qubit = eng.allocate_qubit()
    Rx(0.5) | qubit
    with pytest.raises(Exception) as excinfo:
        qubit[0].__del__()
        eng.flush()

    # verbose=True on the engine re-raises errors instead of compacting them.
    assert type_error is excinfo.value

    # atexit sends another FlushGate, therefore we remove the backend:
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy
Ejemplo n.º 4
0
def test_ibm_sent_error_2(monkeypatch):
    backend = _ibm.IBMBackend(verbose=True)
    mapper = BasicMapperEngine()
    res = dict()
    for i in range(4):
        res[i] = i
    mapper.current_mapping = res
    eng = MainEngine(backend=backend, engine_list=[mapper])
    qubit = eng.allocate_qubit()
    Rx(math.pi) | qubit

    with pytest.raises(Exception):
        S | qubit  # no setup to decompose S gate, so not accepted by the backend
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy
Ejemplo n.º 5
0
def test_ibm_sent_error(monkeypatch):
    # patch send
    def mock_send(*args, **kwargs):
        raise TypeError
    monkeypatch.setattr(_ibm, "send", mock_send)

    backend = _ibm.IBMBackend(verbose=True)
    eng = MainEngine(backend=backend, engine_list=[IBMCNOTMapper()])
    qubit = eng.allocate_qubit()
    X | qubit
    with pytest.raises(Exception):
        qubit[0].__del__()
        eng.flush()
    # atexit sends another FlushGate, therefore we remove the backend:
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy
Ejemplo n.º 6
0
def test_aqt_sent_error(monkeypatch):
    # patch send
    def mock_send(*args, **kwargs):
        raise TypeError

    monkeypatch.setattr(_aqt, "send", mock_send)

    backend = _aqt.AQTBackend(verbose=True)
    eng = MainEngine(backend=backend)
    qubit = eng.allocate_qubit()
    Rx(0.5) | qubit
    with pytest.raises(Exception):
        qubit[0].__del__()
        eng.flush()
    # atexit sends another FlushGate, therefore we remove the backend:
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy
Ejemplo n.º 7
0
def test_ibm_sent_error(monkeypatch):
    # patch send
    def mock_send(*args, **kwargs):
        raise TypeError

    monkeypatch.setattr(_ibm, "send", mock_send)
    backend = _ibm.IBMBackend(verbose=True)
    mapper = BasicMapperEngine()
    res = dict()
    for i in range(4):
        res[i] = i
    mapper.current_mapping = res
    eng = MainEngine(backend=backend, engine_list=[mapper])
    qubit = eng.allocate_qubit()
    Rx(math.pi) | qubit
    with pytest.raises(Exception):
        qubit[0].__del__()
        eng.flush()
    # atexit sends another FlushGate, therefore we remove the backend:
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy
Ejemplo n.º 8
0
def test_backend_get_probabilities_method(matplotlib_setup):
    class MyBackend(BasicEngine):
        def get_probabilities(self, qureg):
            return {'000': 0.5, '111': 0.5}

        def is_available(self, cmd):
            return True

        def receive(self, command_list):
            for cmd in command_list:
                if not isinstance(cmd.gate, FlushGate):
                    assert isinstance(cmd.gate, AllocateQubitGate)

    eng = MainEngine(backend=MyBackend(), verbose=True)
    qureg = eng.allocate_qureg(3)
    eng.flush()
    _, _, prob = histogram(eng.backend, qureg)
    assert prob['000'] == 0.5
    assert prob['111'] == 0.5

    # NB: avoid throwing exceptions when destroying the MainEngine
    eng.next_engine = DummyEngine()
    eng.next_engine.is_last_engine = True
Ejemplo n.º 9
0
def test_ionq_send_nonetype_response_error(monkeypatch, mapper_factory):
    """Test that no return value from "send" will raise a runtime error."""
    # patch send
    mock_send = mock.MagicMock(return_value=None)
    monkeypatch.setattr(_ionq_http_client, "send", mock_send)

    backend = _ionq.IonQBackend()
    eng = MainEngine(
        backend=backend,
        engine_list=[mapper_factory()],
        verbose=True,
    )
    qubit = eng.allocate_qubit()
    Rx(0.5) | qubit
    with pytest.raises(RuntimeError) as excinfo:
        eng.flush()

    # verbose=True on the engine re-raises errors instead of compacting them.
    assert str(excinfo.value) == "Failed to submit job to the server!"

    # atexit sends another FlushGate, therefore we remove the backend:
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy