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))
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())
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
def test_distributed_circuit_execution_controlled_gate(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.CNOT(0, 2)) c = Circuit(4) c.add((gates.H(i) for i in range(dist_c.nglobal, 4))) c.add(gates.CNOT(0, 2)) 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)
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)
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)
def test_transform_queue_more_gates(backend): devices = {"/GPU:0": 2, "/GPU:1": 2} c = DistributedCircuit(4, devices) c.add(gates.H(0)) c.add(gates.H(1)) c.add(gates.CNOT(2, 3)) c.add(gates.CZ(0, 1)) c.add(gates.CNOT(3, 0)) c.add(gates.CNOT(1, 2)) c.queues.qubits = DistributedQubits([2, 3], c.nqubits) tqueue = c.queues.transform(c.queue) assert len(tqueue) == 10 assert isinstance(tqueue[0], gates.H) assert tqueue[0].target_qubits == (0,) assert isinstance(tqueue[1], gates.H) assert tqueue[1].target_qubits == (1,) assert isinstance(tqueue[2], gates.CZ) assert tqueue[2].target_qubits == (1,) assert isinstance(tqueue[3], gates.SWAP) assert set(tqueue[3].target_qubits) == {1, 3} assert isinstance(tqueue[4], gates.CNOT) assert tqueue[4].target_qubits == (1,) assert isinstance(tqueue[5], gates.CNOT) assert tqueue[5].target_qubits == (0,) assert isinstance(tqueue[6], gates.SWAP) assert set(tqueue[6].target_qubits) == {0, 2} assert isinstance(tqueue[7], gates.CNOT) assert tqueue[7].target_qubits == (0,) assert isinstance(tqueue[8], gates.SWAP) assert set(tqueue[8].target_qubits) == {0, 2} assert isinstance(tqueue[9], gates.SWAP) assert set(tqueue[9].target_qubits) == {1, 3}
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)
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)
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)
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
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)
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)) K.assert_allclose(target_state, final_state, atol=1e-7)
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
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())
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()