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()
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()
Пример #3
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()
Пример #4
0
def test_merge():
    q_sim = EQSN()
    id1 = str(1)
    id2 = str(2)
    q_sim.new_qubit(id1)
    q_sim.new_qubit(id2)
    q_sim.merge_qubits(id1, id2)
    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)
    assert m == m2
    q_sim.stop_all()
Пример #6
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_merge():
    q_sim = EQSN()
    id1 = str(1)
    id2 = str(2)
    q_sim.new_qubit(id1)
    q_sim.new_qubit(id2)
    q_sim.merge_qubits(id1, id2)
    q_sim.stop_all()
    print("merging was succesfull")
def test_error_at_creating_EQSN_twice():
    error = False
    i1 = None
    try:
        i1 = EQSN()
        _ = EQSN()
    except:
        error = True
    assert error
    i1.stop_all()
Пример #9
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
Пример #10
0
def test_error_at_creating_EQSN_twice():
    print("Start error at multiple instances test...")
    error = False
    i1 = None
    try:
        i1 = EQSN()
        _ = EQSN()
    except:
        error = True
    assert error
    print("Finished test")
    i1.stop_all()
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_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)
Пример #13
0
def test_5_qubits_gate():
    q_sim = EQSN()
    ids = [str(x) for x in range(5)]
    for i in ids:
        q_sim.new_qubit(i)
    for i in ids:
        q_sim.H_gate(i)
    q_sim.cnot_gate(ids[1], ids[0])
    q_sim.cnot_gate(ids[2], ids[1])
    q_sim.cnot_gate(ids[3], ids[2])
    q_sim.cnot_gate(ids[4], ids[3])
    for i in ids:
        q_sim.measure(i)
    q_sim.stop_all()
Пример #14
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()
Пример #15
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()
Пример #16
0
def test_custom_two_qubit_control_gate_control_not_applied():
    eqsn = EQSN()
    eqsn.new_qubit("100")
    eqsn.new_qubit("200")
    eqsn.new_qubit("300")
    eqsn.X_gate("200")
    # Swap gate
    custom_gate = np.asarray(
        [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
    )
    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 == 0
    assert m1 == 0

    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()