forked from qir-alliance/pyqir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python2qir.py
66 lines (51 loc) · 2.25 KB
/
python2qir.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
#!/usr/bin/env python3
# Transforms a subset of the Python language into QIR, by using:
# - the built-in ast (Asbtract Syntax Tree) library to parse the source code
# - the pyqir-generator package to generate and display QIR
# Here, we transform a Qiskit circuit without using the Qiskit package
import ast
from pyqir import BasicQisBuilder, SimpleModule
def main() -> None:
# Open and parse the input file
with open("python2qir_qiskit_input.py", "r") as source:
tree = ast.parse(source.read())
# Walk the Abstract Syntax Tree (AST) and translate into QIR with pyqir-generator
analyzer = Analyzer()
analyzer.visit(tree)
print("\n\n== Output QIR ==")
print(analyzer.module.ir())
class Analyzer(ast.NodeVisitor):
module: SimpleModule
builder: BasicQisBuilder
def visit_Call(self, node: ast.Call) -> None:
if isinstance(node.func, ast.Name):
name: ast.Name = node.func
if name.id == "QuantumCircuit":
num_qubits = int_value(node.args[0])
num_results = int_value(node.args[1])
self.module = SimpleModule("python2qir", num_qubits, num_results)
self.builder = BasicQisBuilder(self.module.builder)
if isinstance(node.func, ast.Attribute):
attribute: ast.Attribute = node.func
if attribute.attr == "cx":
control = int_value(node.args[0])
target = int_value(node.args[1])
self.builder.cx(self.module.qubits[control], self.module.qubits[target])
if attribute.attr == "h":
qubit = int_value(node.args[0])
self.builder.h(self.module.qubits[qubit])
if attribute.attr == "measure":
qubit = int_value(node.args[0])
bit = int_value(node.args[1])
self.builder.mz(self.module.qubits[qubit], self.module.results[bit])
if attribute.attr == "z":
qubit = int_value(node.args[0])
self.builder.z(self.module.qubits[qubit])
self.generic_visit(node)
def int_value(e: ast.expr) -> int:
assert isinstance(e, ast.Constant)
value = e.value
assert isinstance(value, int)
return value
if __name__ == "__main__":
main()