Beispiel #1
0
def mock_get_devices():
    # Generated from ISA.to_dict.
    acorn = {'1Q': {'0': {}, '1': {}, '2': {}, '3': {'dead': True}, '4': {}, '5': {}, '6': {}, '7': {}, '8': {},
                    '9': {}, '10': {}, '11': {}, '12': {}, '13': {}, '14': {}, '15': {}, '16': {}, '17': {}, '18': {},
                    '19': {}},
     '2Q': {'0-5': {}, '0-6': {}, '1-6': {}, '1-7': {}, '2-7': {}, '2-8': {}, '4-9': {}, '5-10': {}, '6-11': {},
            '7-12': {}, '8-13': {}, '9-14': {}, '10-15': {}, '10-16': {}, '11-16': {}, '11-17': {}, '12-17': {},
            '12-18': {}, '13-18': {}, '13-19': {}, '14-19': {}}}
    agave = {'1Q': {'0': {}, '1': {}, '2': {}, '3': {}, '4': {}, '5': {}, '6': {}, '7': {}},
             '2Q': {'0-1': {}, '1-2': {}, '2-3': {}, '3-4': {}, '4-5': {}, '5-6': {}, '6-7': {}, '7-0': {}}}
    mock_acorn = Mock(spec=Device)
    mock_agave = Mock(spec=Device)
    mock_acorn.isa = ISA.from_dict(acorn)
    mock_agave.isa = ISA.from_dict(agave)

    # Make sure we are matching the signature.
    mocked_function = create_autospec(get_devices)

    def side_effect(as_dict=True):
        if as_dict:
            return {'19Q-Acorn': mock_acorn,
                    '8Q-Agave': mock_agave}
        else:
            return {acorn, agave}
    mocked_function.side_effect = side_effect
    return mocked_function
Beispiel #2
0
def test_isa(isa_dict):
    isa = ISA.from_dict(isa_dict)
    assert isa == ISA(qubits=[
        Qubit(id=0, type='Xhalves', dead=False),
        Qubit(id=1, type='Xhalves', dead=False),
        Qubit(id=2, type='Xhalves', dead=False),
        Qubit(id=3, type='Xhalves', dead=True),
    ],
                      edges=[
                          Edge(targets=[0, 1], type='CZ', dead=False),
                          Edge(targets=[0, 2], type='CPHASE', dead=False),
                          Edge(targets=[0, 3], type='CZ', dead=True),
                          Edge(targets=[1, 2], type='ISWAP', dead=False),
                      ])
    assert isa == ISA.from_dict(isa.to_dict())
Beispiel #3
0
def compiler():
    try:
        compiler = CompilerConnection(isa_source=ISA.from_dict(isa_dict()))
        compiler.compile(Program(I(0)))
        return compiler
    except (RequestException, UnknownApiError) as e:
        return pytest.skip(
            "This test requires compiler connection: {}".format(e))
Beispiel #4
0
def test_isa(isa_dict):
    isa = ISA.from_dict(isa_dict)
    assert isa == ISA(
        qubits=[
            Qubit(id=0, type="Xhalves", dead=False),
            Qubit(id=1, type="Xhalves", dead=False),
            Qubit(id=2, type="Xhalves", dead=False),
            Qubit(id=3, type="Xhalves", dead=True),
        ],
        edges=[
            Edge(targets=[0, 1], type="CZ", dead=False),
            Edge(targets=[0, 2], type="CPHASE", dead=False),
            Edge(targets=[0, 3], type="CZ", dead=True),
            Edge(targets=[1, 2], type="ISWAP", dead=False),
        ],
    )
    assert isa == ISA.from_dict(isa.to_dict())
def test_isa(isa_dict):
    isa = ISA.from_dict(isa_dict)
    assert isa == ISA(name=isa_dict['id']['name'],
                      version=isa_dict['id']['version'],
                      timestamp=isa_dict['id']['timestamp'],
                      qubits=[
                          Qubit(id=0, type='Xhalves', dead=False),
                          Qubit(id=1, type='Xhalves', dead=False),
                          Qubit(id=2, type='Xhalves', dead=False),
                          Qubit(id=3, type='Xhalves', dead=True),
                      ],
                      edges=[
                          Edge(targets=[0, 1], type='CZ', dead=False),
                          Edge(targets=[1, 2], type='ISWAP', dead=False),
                          Edge(targets=[2, 0], type='CPHASE', dead=False),
                          Edge(targets=[0, 3], type='CZ', dead=True),
                      ])
    assert isa == ISA.from_dict(isa.to_dict())
Beispiel #6
0
def test_NxDevice(isa_dict, noise_model_dict):
    graph = isa_to_graph(ISA.from_dict(isa_dict))
    nxdev = NxDevice(graph)

    device_raw = {'isa': isa_dict,
                  'noise_model': noise_model_dict,
                  'is_online': True,
                  'is_retuning': False}
    dev = Device(DEVICE_FIXTURE_NAME, device_raw)

    nx.is_isomorphic(nxdev.qubit_topology(), dev.qubit_topology())
    isa = nxdev.get_isa()
    assert isa.qubits[0].type == 'Xhalves'
def test_gates_in_isa(isa_dict):
    isa = ISA.from_dict(isa_dict)
    gates = gates_in_isa(isa)
    for q in [0, 1, 2]:
        for g in [I, RX(np.pi / 2), RX(-np.pi / 2), RZ(THETA)]:
            assert g(q) in gates

    assert CZ(0, 1) in gates
    assert CZ(1, 0) in gates
    assert ISWAP(1, 2) in gates
    assert ISWAP(2, 1) in gates
    assert CPHASE(THETA)(2, 0) in gates
    assert CPHASE(THETA)(0, 2) in gates
Beispiel #8
0
def test_NxDevice(isa_dict, noise_model_dict):
    graph = isa_to_graph(ISA.from_dict(isa_dict))
    nxdev = NxDevice(graph)

    device_raw = {
        "isa": isa_dict,
        "noise_model": noise_model_dict,
        "is_online": True,
        "is_retuning": False,
    }
    dev = Device(DEVICE_FIXTURE_NAME, device_raw)

    nx.is_isomorphic(nxdev.qubit_topology(), dev.qubit_topology())
    isa = nxdev.get_isa()
    assert isa.qubits[0].type == "Xhalves"
Beispiel #9
0
def test_device(isa_dict, noise_model_dict):
    device_raw = {'isa': isa_dict,
                  'noise_model': noise_model_dict,
                  'is_online': True,
                  'is_retuning': False}

    device = Device(DEVICE_FIXTURE_NAME, device_raw)
    assert device.name == DEVICE_FIXTURE_NAME

    isa = ISA.from_dict(isa_dict)
    noise_model = NoiseModel.from_dict(noise_model_dict)
    # Device.isa is deprecated, but seemingly this is what we want here
    with pytest.warns(DeprecationWarning):
        assert isinstance(device.isa, ISA)
        assert device.isa == isa
    assert isinstance(device.noise_model, NoiseModel)
    assert device.noise_model == noise_model
Beispiel #10
0
def test_device(isa_dict, noise_model_dict):
    device_raw = {
        'isa': isa_dict,
        'noise_model': noise_model_dict,
        'is_online': True,
        'is_retuning': False
    }

    device = Device(DEVICE_FIXTURE_NAME, device_raw)
    assert device.name == DEVICE_FIXTURE_NAME

    isa = ISA.from_dict(isa_dict)
    noise_model = NoiseModel.from_dict(noise_model_dict)
    assert isinstance(device.isa, ISA)
    assert device.isa == isa
    assert isinstance(device.noise_model, NoiseModel)
    assert device.noise_model == noise_model
Beispiel #11
0
def test_isa_to_graph(isa_dict):
    graph = isa_to_graph(ISA.from_dict(isa_dict))
    should_be = nx.from_edgelist([(0, 1), (1, 2), (0, 2)])
    assert nx.is_isomorphic(graph, should_be)
Beispiel #12
0
COMPILED_BELL_STATE = Program([
    Pragma("EXPECTED_REWIRING", ('"#(0 1 2 3)"',)),
    RZ(pi / 2, 0),
    RX(pi / 2, 0),
    RZ(-pi / 2, 1),
    RX(pi / 2, 1),
    CZ(1, 0),
    RZ(-pi / 2, 0),
    RX(-pi / 2, 1),
    RZ(pi / 2, 1),
    Pragma("CURRENT_REWIRING", ('"#(0 1 2 3)"',)),
    Pragma("EXPECTED_REWIRING", ('"#(0 1 2 3)"',)),
    Pragma("CURRENT_REWIRING", ('"#(0 1 2 3)"',)),
])
DUMMY_ISA_DICT = {"1Q": {"0": {}, "1": {}}, "2Q": {"0-1": {}}}
DUMMY_ISA = ISA.from_dict(DUMMY_ISA_DICT)

mock_qvm = QVMConnection(api_key='api_key', user_id='user_id')
mock_async_qvm = QVMConnection(api_key='api_key', user_id='user_id', use_queue=True)
mock_compiler = CompilerConnection(isa_source=DUMMY_ISA, api_key='api_key', user_id='user_id')
mock_async_compiler = CompilerConnection(isa_source=DUMMY_ISA, api_key='api_key', user_id='user_id', use_queue=True)


def test_sync_run_mock():
    def mock_response(request, context):
        assert json.loads(request.text) == {
            "type": "multishot",
            "addresses": [0, 1],
            "trials": 2,
            "compiled-quil": "H 0\nCNOT 0 1\nMEASURE 0 [0]\nMEASURE 1 [1]\n"
        }