Ejemplo n.º 1
0
def test_custom_controlled_gate():
    eqsn = EQSN()
    gate = np.array([[0, 1],[1,0]], dtype=np.csingle)
    eqsn.new_qubit('1')
    eqsn.new_qubit('2')
    eqsn.H_gate('1')
    eqsn.custom_controlled_gate('2', '1', gate)
    res1 = eqsn.measure('1')
    res2 = eqsn.measure('2')
    eqsn.stop_all()
    assert res1 == res2
Ejemplo n.º 2
0
def test_custom_single_gate():
    eqsn = EQSN()
    gate = (1/2)**0.5 * np.array([[1, 1],[1,-1]], dtype=np.csingle)
    eqsn.new_qubit('1')
    eqsn.custom_gate('1', gate)
    eqsn.custom_gate('1', gate)
    res = eqsn.measure('1')
    eqsn.stop_all()
    assert res == 0
def test_two_qubit_gate():
    eqsn = EQSN()
    eqsn.new_qubit("1")
    eqsn.new_qubit("2")
    eqsn.X_gate("1")
    custom_gate = np.asarray([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0],
                              [0, 0, 0, 1]])
    eqsn.custom_two_qubit_gate("1", "2", custom_gate)
    m1 = eqsn.measure("1")
    m2 = eqsn.measure("2")
    assert m1 == 0
    assert m2 == 1
    eqsn.stop_all()
def test_measure_from_threads():
    q_sim = EQSN()

    def measure_or_hadamard(_id):
        n = random.randrange(10, 20, 1)
        for _ in range(n):
            time.sleep(0.1)
            q_sim.H_gate(_id)

    nr_threads = 10
    ids = [str(x) for x in range(nr_threads)]
    for _id in ids:
        q_sim.new_qubit(_id)
    id1 = ids[0]

    for c in ids:
        if c != id1:
            q_sim.cnot_gate(id1, c)

    thread_list = []
    for _id in ids:
        t = threading.Thread(target=measure_or_hadamard, args=(_id, ))
        t.start()
        thread_list.append(t)

    for t in thread_list:
        t.join()
    q_sim.stop_all()
Ejemplo n.º 5
0
def test_measure_from_threads():
    q_sim = EQSN()

    def measure_or_hadamard(id):
        n = random.randrange(10, 100, 1)
        for _ in range(n):
            # time.sleep(0.05)
            q_sim.H_gate(id)
        print("Finished Hadamard, measure qubit %s!" % id)
        print(q_sim.measure(id))
        print("Finished with Measure!")

    nr_threads = 10
    ids = [str(x) for x in range(nr_threads)]
    for id in ids:
        q_sim.new_qubit(id)
    id1 = ids[0]
    for c in ids:
        if c != id1:
            q_sim.cnot_gate(id1, c)
    thread_list = []
    for id in ids:
        print(id)
        t = threading.Thread(target=measure_or_hadamard, args=(id, ))
        t.start()
        thread_list.append(t)
    for t in thread_list:
        t.join()
    print("Test was successfull!")
    q_sim.stop_all()
def test_non_destructive_measurement():
    q_sim = EQSN()
    id1 = str(1)
    q_sim.new_qubit(id1)
    q_sim.H_gate(id1)
    m = q_sim.measure(id1, non_destructive=True)
    m2 = q_sim.measure(id1)
    print("Measured %d." % m)
    assert m == m2
    print("Test was successfull!")
    q_sim.stop_all()
    print("Stopped succesfully!")
    exit(0)
Ejemplo n.º 7
0
def test_H_gate():
    q_sim = EQSN.get_instance()
    id = str(10)
    q_sim.new_qubit(id)
    q_sim.H_gate(id)
    q_sim.H_gate(id)
    res = q_sim.measure(id)
    assert res == 0
    q_sim.stop_all()
Ejemplo n.º 8
0
def test_x_gate():
    q_sim = EQSN.get_instance()
    id = str(10)
    q_sim.new_qubit(id)
    q_sim.X_gate(id)
    res = q_sim.measure(id)
    print("measured " + str(res))
    assert res == 1
    q_sim.stop_all()
Ejemplo n.º 9
0
def test_S_gate():
    q_sim = EQSN.get_instance()
    id = str(11)
    q_sim.new_qubit(id)
    q_sim.H_gate(id)
    q_sim.S_gate(id)
    q_sim.S_gate(id)
    q_sim.H_gate(id)
    res = q_sim.measure(id)
    print("measured %d." % res)
    assert res == 1
    q_sim.stop_all()
Ejemplo n.º 10
0
def test_call_single_qubit_gate_from_threads():
    eqsn = EQSN()

    def call_X_gate_n_times(_id, n):
        for _ in range(n):
            eqsn.X_gate(_id)

    id1 = str(1)
    eqsn.new_qubit(id1)
    n = 99
    nr_threads = 5
    thread_list = []
    for _ in range(nr_threads):
        t = threading.Thread(target=call_X_gate_n_times, args=(id1, n))
        t.start()
        thread_list.append(t)
    for t in thread_list:
        t.join()
    m = eqsn.measure(id1)
    assert m == 1
    eqsn.stop_all()
def test_call_single_qubit_gate_from_threads():
    q_sim = EQSN()

    def call_X_gate_n_times(id, n):
        for c in range(n):
            # print("Apply %d time." % c)
            q_sim.X_gate(id)

    id1 = str(1)
    q_sim.new_qubit(id1)
    n = 999
    nr_threads = 5
    thread_list = []
    for _ in range(nr_threads):
        t = threading.Thread(target=call_X_gate_n_times, args=(id1, n))
        t.start()
        thread_list.append(t)
    for t in thread_list:
        t.join()
    m = q_sim.measure(id1)
    print("Measured %d." % m)
    assert m == 1
    print("Test was successfull!")
    q_sim.stop_all()
Ejemplo n.º 12
0
from eqsn import EQSN

if __name__ == "__main__":
    eqsn = EQSN()
    id = "Qubit"
    eqsn.new_qubit(id)
    m = eqsn.measure(id)
    print("Measured Qubit with result %d." % m)
Ejemplo n.º 13
0
 def __init__(self):
     self._hosts = EQSNBackend.Hosts.get_instance()
     # keys are from : to, where from is the host calling create EPR
     self._entaglement_qubits = EQSNBackend.EntanglementIDs.get_instance()
     self.eqsn = EQSN.get_instance()
Ejemplo n.º 14
0
def test_custom_two_qubit_control_gate_other_qubits_not_affected():
    eqsn = EQSN()

    eqsn.new_qubit("400")
    eqsn.new_qubit("200")
    eqsn.new_qubit("300")
    eqsn.new_qubit("100")
    eqsn.new_qubit("500")

    eqsn.X_gate("100")
    eqsn.X_gate("200")
    eqsn.X_gate("500")

    # CNOT gate
    custom_gate = np.asarray(
        [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]
    )
    eqsn.custom_two_qubit_control_gate("100", "200", "500", custom_gate)

    m1 = eqsn.measure("100")
    m2 = eqsn.measure("200")
    m3 = eqsn.measure("300")
    m4 = eqsn.measure("400")
    m5 = eqsn.measure("500")

    assert m2 == 1
    assert m3 == 0
    assert m1 == 1
    assert m4 == 0
    assert m5 == 0

    eqsn.stop_all()
Ejemplo n.º 15
0
def test_custom_two_qubit_control_gate_control_ccnot():
    eqsn = EQSN()
    eqsn.new_qubit("100")
    eqsn.new_qubit("200")
    eqsn.new_qubit("300")

    eqsn.X_gate("100")
    eqsn.X_gate("200")
    # Swap gate
    custom_gate = np.asarray(
        [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]
    )
    eqsn.custom_two_qubit_control_gate("100", "200", "300", custom_gate)

    m1 = eqsn.measure("100")
    m2 = eqsn.measure("200")
    m3 = eqsn.measure("300")

    assert m2 == 1
    assert m3 == 1
    assert m1 == 1

    eqsn.stop_all()
Ejemplo n.º 16
0
def main():
    # Activate debugging
    logging.basicConfig(level=logging.DEBUG)
    eqsn = EQSN()

    # create and measure
    id = "Qubit"
    eqsn.new_qubit(id)
    _ = eqsn.measure(id)

    # merge
    id1 = str(1)
    id2 = str(2)
    eqsn.new_qubit(id1)
    eqsn.new_qubit(id2)
    eqsn.merge_qubits(id1, id2)

    eqsn.stop_all()
Ejemplo n.º 17
0
from eqsn import EQSN

if __name__ == "__main__":
    eqsn = EQSN()
    eqsn.new_qubit('A')
    eqsn.new_qubit('B')
    eqsn.H_gate('A')
    eqsn.cnot_gate('B', 'A')
    m1 = eqsn.measure('A')
    m2 = eqsn.measure('B')
    print("Measured entangled pair with results %d and %d." % (m1, m2))