-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic_gates_creator.py
executable file
·80 lines (75 loc) · 2.29 KB
/
logic_gates_creator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
This program creates a .tex file of several classical logic gates
'''
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
import os
import shutil
logic_folder = os.getcwd()
LaTex_folder_logic_gates = str(os.getcwd())+'/Latex_quantum_gates/logic_gates/'
if not os.path.exists(LaTex_folder_logic_gates):
os.makedirs(LaTex_folder_logic_gates)
else:
shutil.rmtree(LaTex_folder_logic_gates)
os.makedirs(LaTex_folder_logic_gates)
print(LaTex_folder_logic_gates)
## NOT gate
q = QuantumRegister(1, name='q')
c = ClassicalRegister(1, name='c')
qc = QuantumCircuit(q,c)
qc.x(q[0])
qc.measure(q[0],c[0])
LaTex_code = qc.draw(output='latex_source', justify=None) # draw the circuit
f_name = 'NOT_gate.tex'
with open(LaTex_folder_logic_gates+f_name, 'w') as f:
f.write(LaTex_code)
## AND gate
q = QuantumRegister(3, name='q')
qubit_state = '0'
c = ClassicalRegister(1, name='c')
qc = QuantumCircuit(q,c)
qc.reset(q[2])
qc.ccx(q[0], q[1], q[2])
qc.measure(q[2], c[0])
LaTex_code = qc.draw(output='latex_source', justify=None) # draw the circuit
f_name = 'AND_gate.tex'
with open(LaTex_folder_logic_gates+f_name, 'w') as f:
f.write(LaTex_code)
## OR gate
q = QuantumRegister(3, name='q')
c = ClassicalRegister(1, name='c')
qc = QuantumCircuit(q,c)
qc.reset(q[2])
qc.ccx(q[0], q[1], q[2])
qc.cx(q[0],q[2])
qc.cx(q[1],q[2])
qc.measure(q[2],c[0])
LaTex_code = qc.draw(output='latex_source', justify=None) # draw the circuit
f_name = 'OR_gate.tex'
with open(LaTex_folder_logic_gates+f_name, 'w') as f:
f.write(LaTex_code)
## XOR gate
q = QuantumRegister(3, name='q')
c = ClassicalRegister(1, name='c')
qc = QuantumCircuit(q,c)
qc.reset(q[2])
qc.cx(q[0],q[2])
qc.cx(q[1],q[2])
qc.measure(q[2],c[0])
LaTex_code = qc.draw(output='latex_source', justify=None) # draw the circuit
f_name = 'XOR_gate.tex'
with open(LaTex_folder_logic_gates+f_name, 'w') as f:
f.write(LaTex_code)
## NOR gate
q = QuantumRegister(3, name='q')
c = ClassicalRegister(1, name='c')
qc = QuantumCircuit(q,c)
qc.reset(q[2])
qc.ccx(q[0], q[1], q[2])
qc.cx(q[0],q[2])
qc.cx(q[1],q[2])
qc.x(q[2])
qc.measure(q[2],c[0])
LaTex_code = qc.draw(output='latex_source', justify=None) # draw the circuit
f_name = 'NOR_gate.tex'
with open(LaTex_folder_logic_gates+f_name, 'w') as f:
f.write(LaTex_code)