Exemplo n.º 1
0
def test_distributed_circuit_get_initial_state_random(backend, accelerators):
    import itertools
    from qibo.tests.utils import random_state
    target_state = random_state(5)
    c = DistributedCircuit(5, accelerators)
    c.queues.qubits = DistributedQubits(range(c.nglobal), c.nqubits)
    state = c.get_initial_state(target_state)
    np.testing.assert_allclose(state, target_state)

    target_state = np.reshape(target_state, 5 * (2, ))
    for i, s in enumerate(itertools.product([0, 1], repeat=c.nglobal)):
        target_piece = target_state[s].flatten()
        np.testing.assert_allclose(target_piece.ravel(), state.pieces[i])
Exemplo n.º 2
0
def test_distributed_circuit_empty_execution(backend, accelerators):
    # test executing a circuit with the default initial state
    c = DistributedCircuit(5, accelerators)
    final_state = c().state()
    target_state = np.zeros_like(final_state)
    target_state[0] = 1
    K.assert_allclose(final_state, target_state)
    # test re-executing the circuit with a given initial state
    initial_state = random_state(c.nqubits)
    K.assert_allclose(c(initial_state), initial_state)
    # test executing a new circuit with a given initial state
    c = DistributedCircuit(5, accelerators)
    initial_state = random_state(c.nqubits)
    K.assert_allclose(c(initial_state), initial_state)
    # test re-executing the circuit with the default initial state
    K.assert_allclose(c(), target_state)
Exemplo n.º 3
0
def test_transform_queue_simple(backend):
    devices = {"/GPU:0": 1, "/GPU:1": 1}
    c = DistributedCircuit(4, devices)
    c.add((gates.H(i) for i in range(4)))
    c.queues.qubits = DistributedQubits([0], c.nqubits)
    tqueue = c.queues.transform(c.queue)
    assert len(tqueue) == 6
    for i in range(3):
        assert isinstance(tqueue[i], gates.H)
        assert tqueue[i].target_qubits == (i + 1,)
    assert isinstance(tqueue[3], gates.SWAP)
    assert tqueue[3].target_qubits == (0, 1)
    assert isinstance(tqueue[4], gates.H)
    assert tqueue[4].target_qubits == (1,)
    assert isinstance(tqueue[5], gates.SWAP)
    assert tqueue[5].target_qubits == (0, 1)
Exemplo n.º 4
0
def test_create_queue_errors(backend):
    c = DistributedCircuit(4, {"/GPU:0": 2})
    c.add(gates.H(0))
    c.add(gates.H(1))
    c.queues.qubits = DistributedQubits([0], c.nqubits)
    with pytest.raises(ValueError):
        c.queues.create(c.queue)

    c = DistributedCircuit(4, {"/GPU:0": 4})
    c.add(gates.SWAP(0, 1))
    c.queues.qubits = DistributedQubits([0, 1], c.nqubits)
    with pytest.raises(ValueError):
        c.queues.create(c.queue)
Exemplo n.º 5
0
def test_distributed_circuit_execution_special_gate(backend, accelerators):
    dist_c = DistributedCircuit(6, accelerators)
    initial_state = random_state(dist_c.nqubits)
    dist_c.add(gates.Flatten(np.copy(initial_state)))
    dist_c.add((gates.H(i) for i in range(dist_c.nlocal)))
    dist_c.global_qubits = range(dist_c.nlocal, dist_c.nqubits)
    c = Circuit(6)
    c.add(gates.Flatten(np.copy(initial_state)))
    c.add((gates.H(i) for i in range(dist_c.nlocal)))
    np.testing.assert_allclose(dist_c(), c())
Exemplo n.º 6
0
def test_distributed_circuit_add_gate(backend):
    # Attempt to add gate so that available global qubits are not enough
    c = DistributedCircuit(2, {"/GPU:0": 2})
    with pytest.raises(ValueError):
        c.add(gates.SWAP(0, 1))
    # Attempt adding noise channel
    with pytest.raises(NotImplementedError):
        c.add(gates.PauliNoiseChannel(0, px=0.1, pz=0.1))
Exemplo n.º 7
0
def test_distributed_circuit_execution_pretransformed(backend, accelerators):
    dist_c = DistributedCircuit(4, accelerators)
    dist_c.add((gates.H(i) for i in range(dist_c.nglobal, 4)))
    dist_c.add(gates.SWAP(0, 2))
    dist_c.add((gates.H(i) for i in range(dist_c.nglobal, 4)))

    c = Circuit(4)
    c.add((gates.H(i) for i in range(dist_c.nglobal, 4)))
    c.add(gates.SWAP(0, 2))
    c.add((gates.H(i) for i in range(dist_c.nglobal, 4)))

    initial_state = random_state(c.nqubits)
    final_state = dist_c(np.copy(initial_state))
    target_state = c(np.copy(initial_state))
    np.testing.assert_allclose(target_state, final_state)
Exemplo n.º 8
0
def test_distributed_circuit_execution_with_swap(backend, accelerators):
    dist_c = DistributedCircuit(6, accelerators)
    dist_c.add((gates.H(i) for i in range(6)))
    dist_c.add((gates.SWAP(i, i + 1) for i in range(5)))
    dist_c.global_qubits = [0, 1]

    c = Circuit(6)
    c.add((gates.H(i) for i in range(6)))
    c.add((gates.SWAP(i, i + 1) for i in range(5)))

    initial_state = random_state(c.nqubits)
    final_state = dist_c(np.copy(initial_state))
    target_state = c(np.copy(initial_state))
    np.testing.assert_allclose(target_state, final_state)
Exemplo n.º 9
0
def test_distributed_circuit_various_errors(backend):
    devices = {"/GPU:0": 2, "/GPU:1": 2}
    c = DistributedCircuit(5, devices)
    # Attempt to use ``.with_noise``
    with pytest.raises(NotImplementedError):
        c.with_noise((0.1, 0.2, 0.1))
    # Attempt to compile
    with pytest.raises(RuntimeError):
        c.compile()
    # Attempt to access state before being set
    with pytest.raises(RuntimeError):
        final_state = c.final_state
Exemplo n.º 10
0
def test_create_queue_with_global_swap(backend):
    devices = {"/GPU:0": 2, "/GPU:1": 2}
    c = DistributedCircuit(6, devices)
    c.add([gates.H(0), gates.H(2), gates.H(3)])
    c.add(gates.SWAP(3, 4))
    c.add([gates.X(1), gates.X(2)])
    c.queues.qubits = DistributedQubits([4, 5], c.nqubits)
    c.queues.create(c.queues.transform(c.queue))

    check_device_queues(c.queues)
    assert len(c.queues.special_queue) == 1
    assert len(c.queues.queues) == 3
    assert len(c.queues.queues[0]) == 4
    assert len(c.queues.queues[1]) == 0
    assert len(c.queues.queues[2]) == 4
    for device_group in c.queues.queues[0]:
        assert len(device_group) == 3
    for device_group in c.queues.queues[2]:
        assert len(device_group) == 2
Exemplo n.º 11
0
def test_distributed_circuit_execution(backend, accelerators,
                                       use_global_qubits):
    dist_c = DistributedCircuit(6, accelerators)
    c = Circuit(6)
    if use_global_qubits:
        dist_c.add((gates.H(i) for i in range(dist_c.nqubits)))
        c.add((gates.H(i) for i in range(dist_c.nqubits)))
    else:
        dist_c.add((gates.H(i) for i in range(dist_c.nlocal)))
        c.add((gates.H(i) for i in range(dist_c.nlocal)))
    dist_c.global_qubits = range(dist_c.nlocal, dist_c.nqubits)

    initial_state = random_state(c.nqubits)
    final_state = dist_c(np.copy(initial_state))
    target_state = c(np.copy(initial_state))
    np.testing.assert_allclose(target_state, final_state)
Exemplo n.º 12
0
def test_distributed_circuit_set_gates(backend):
    devices = {"/GPU:0": 2, "/GPU:1": 2}
    c = DistributedCircuit(6, devices)
    c.add((gates.H(i) for i in range(4)))
    c.queues.set(c.queue)

    check_device_queues(c.queues)
    assert len(c.queues.queues) == 1
    assert len(c.queues.queues[0]) == 4
    for queues in c.queues.queues[0]:
        assert len(queues) == 4

    # Attempt to set gates before adding any gate
    c = DistributedCircuit(6, devices)
    with pytest.raises(RuntimeError):
        c.queues.set(c.queue)
Exemplo n.º 13
0
def test_distributed_circuit_set_gates_controlled(backend):
    devices = {"/GPU:0": 2, "/GPU:1": 2}
    c = DistributedCircuit(6, devices)
    c.add([gates.H(0), gates.H(2), gates.H(3)])
    c.add(gates.CNOT(4, 5))
    c.add(gates.Z(1).controlled_by(0))
    c.add(gates.SWAP(2, 3))
    c.add([gates.X(2), gates.X(3), gates.X(4)])
    c.queues.set(c.queue)

    check_device_queues(c.queues)
    assert len(c.queues.queues) == 7
    for i, queue in enumerate(c.queues.queues[:-2]):
        assert len(queue) == 4 * (1 - i % 2)
    for device_group in c.queues.queues[0]:
        assert len(device_group) == 7
    for device_group in c.queues.queues[2]:
        assert len(device_group) == 1
Exemplo n.º 14
0
def test_distributed_circuit_fusion(backend, accelerators):
    c = DistributedCircuit(4, accelerators)
    c.add((gates.H(i) for i in range(4)))
    with pytest.raises(NotImplementedError):
        c.fuse()
Exemplo n.º 15
0
def test_distributed_circuit_execution_controlled_by_gates(
        backend, accelerators):
    dist_c = DistributedCircuit(6, accelerators)
    dist_c.add([gates.H(0), gates.H(2), gates.H(3)])
    dist_c.add(gates.CNOT(4, 5))
    dist_c.add(gates.Z(1).controlled_by(0))
    dist_c.add(gates.SWAP(2, 3))
    dist_c.add([gates.X(2), gates.X(3), gates.X(4)])

    c = Circuit(6)
    c.add([gates.H(0), gates.H(2), gates.H(3)])
    c.add(gates.CNOT(4, 5))
    c.add(gates.Z(1).controlled_by(0))
    c.add(gates.SWAP(2, 3))
    c.add([gates.X(2), gates.X(3), gates.X(4)])

    initial_state = random_state(c.nqubits)
    final_state = dist_c(np.copy(initial_state))
    target_state = c(np.copy(initial_state))
    np.testing.assert_allclose(target_state, final_state)
Exemplo n.º 16
0
def test_distributed_circuit_execution_addition(backend, accelerators):
    # Attempt to add circuits with different devices
    c1 = DistributedCircuit(6, {"/GPU:0": 2, "/GPU:1": 2})
    c2 = DistributedCircuit(6, {"/GPU:0": 2})
    with pytest.raises(ValueError):
        c = c1 + c2

    c1 = DistributedCircuit(6, accelerators)
    c2 = DistributedCircuit(6, accelerators)
    c1.add([gates.H(i) for i in range(6)])
    c2.add([gates.CNOT(i, i + 1) for i in range(5)])
    c2.add([gates.Z(i) for i in range(6)])
    dist_c = c1 + c2

    c = Circuit(6)
    c.add([gates.H(i) for i in range(6)])
    c.add([gates.CNOT(i, i + 1) for i in range(5)])
    c.add([gates.Z(i) for i in range(6)])
    assert c.depth == dist_c.depth
    np.testing.assert_allclose(dist_c(), c())