Skip to content

Commit eb31ea0

Browse files
authored
Merge pull request #8 from tqsd/fix_multiple_instance_issue
Fix multiple instance issue
2 parents 7c23dcb + da42f81 commit eb31ea0

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

eqsn/gates.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ class EQSN(object):
1515
All functions are threadsafe, but at the moment, only one instance should be
1616
used.
1717
"""
18+
__instance = None
19+
20+
@staticmethod
21+
def get_instance():
22+
if EQSN.__instance is None:
23+
return EQSN()
24+
return EQSN.__instance
1825

1926
def __init__(self):
27+
if EQSN.__instance is not None:
28+
raise ValueError("Use get instance to get this class")
29+
EQSN.__instance = self
2030
self.manager = multiprocessing.Manager()
2131
self.shared_dict = SharedDict.get_instance()
2232
cpu_count = multiprocessing.cpu_count()
@@ -51,6 +61,7 @@ def stop_all(self):
5161
p.join()
5262
self.shared_dict.stop_shared_dict()
5363
self.process_picker.stop_process_picker()
64+
EQSN.__instance = None
5465

5566
def X_gate(self, q_id):
5667
"""

tests/test_multi_gate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def test_epr_creation():
6-
q_sim = EQSN()
6+
q_sim = EQSN.get_instance()
77
id1 = str(1)
88
id2 = str(2)
99
q_sim.new_qubit(id1)

tests/test_multiple_instances_eqsn.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from eqsn import EQSN
2+
import time
3+
4+
5+
def test_get_instance():
6+
print("test get instance...")
7+
i1 = EQSN.get_instance()
8+
i2 = EQSN.get_instance()
9+
assert i1 == i2
10+
i1.stop_all()
11+
i2.stop_all()
12+
print("Test succesfull")
13+
14+
15+
def test_error_at_creating_EQSN_twice():
16+
print("Start error at multiple instances test...")
17+
error = False
18+
i1 = None
19+
try:
20+
i1 = EQSN()
21+
_ = EQSN()
22+
except:
23+
error = True
24+
assert error
25+
print("Finished test")
26+
i1.stop_all()
27+
28+
29+
if __name__ == "__main__":
30+
test_get_instance()
31+
time.sleep(0.1)
32+
test_error_at_creating_EQSN_twice()

tests/test_single_gate.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44

55
def test_x_gate():
6-
q_sim = EQSN()
6+
q_sim = EQSN.get_instance()
77
id = str(10)
88
q_sim.new_qubit(id)
99
q_sim.X_gate(id)
@@ -14,8 +14,8 @@ def test_x_gate():
1414

1515

1616
def test_y_gate():
17-
q_sim = EQSN()
18-
id = str(10)
17+
q_sim = EQSN.get_instance()
18+
id = str(11)
1919
q_sim.new_qubit(id)
2020
q_sim.Y_gate(id)
2121
q_sim.Y_gate(id)
@@ -26,7 +26,7 @@ def test_y_gate():
2626

2727

2828
def test_z_gate():
29-
q_sim = EQSN()
29+
q_sim = EQSN.get_instance()
3030
id = str(10)
3131
q_sim.new_qubit(id)
3232
q_sim.Z_gate(id)
@@ -37,7 +37,7 @@ def test_z_gate():
3737

3838

3939
def test_H_gate():
40-
q_sim = EQSN()
40+
q_sim = EQSN.get_instance()
4141
id = str(10)
4242
q_sim.new_qubit(id)
4343
q_sim.H_gate(id)
@@ -48,7 +48,7 @@ def test_H_gate():
4848

4949

5050
def test_T_gate():
51-
q_sim = EQSN()
51+
q_sim = EQSN.get_instance()
5252
id = str(10)
5353
q_sim.new_qubit(id)
5454
q_sim.T_gate(id)
@@ -70,7 +70,7 @@ def test_T_gate():
7070

7171

7272
def test_S_gate():
73-
q_sim = EQSN()
73+
q_sim = EQSN.get_instance()
7474
id = str(11)
7575
q_sim.new_qubit(id)
7676
q_sim.H_gate(id)
@@ -84,7 +84,7 @@ def test_S_gate():
8484

8585

8686
def test_K_gate():
87-
q_sim = EQSN()
87+
q_sim = EQSN.get_instance()
8888
print("test K gate.")
8989
id = str(11)
9090
q_sim.new_qubit(id)
@@ -99,7 +99,7 @@ def test_K_gate():
9999

100100

101101
def test_measure():
102-
q_sim = EQSN()
102+
q_sim = EQSN.get_instance()
103103
id = str(10)
104104
q_sim.new_qubit(id)
105105
res = q_sim.measure(id)
@@ -118,4 +118,4 @@ def test_measure():
118118
test_measure]
119119
for func in test_list:
120120
func()
121-
time.sleep(0.05)
121+
time.sleep(0.1)

0 commit comments

Comments
 (0)