forked from qir-alliance/pyqir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_result.py
40 lines (29 loc) · 1.13 KB
/
if_result.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from pyqir import BasicQisBuilder, SimpleModule
mod = SimpleModule("if_result", num_qubits=2, num_results=2)
qis = BasicQisBuilder(mod.builder)
# Manually reset a qubit by measuring it and applying the X gate if the result
# is one.
qis.h(mod.qubits[0])
qis.mz(mod.qubits[0], mod.results[0])
qis.if_result(mod.results[0], lambda: qis.x(mod.qubits[0]))
# Branches can be nested, for example, to execute an instruction only if both
# results are one.
for i in range(2):
qis.h(mod.qubits[i])
qis.mz(mod.qubits[i], mod.results[i])
def x_both() -> None:
qis.x(mod.qubits[0])
qis.x(mod.qubits[1])
qis.if_result(mod.results[0], lambda: qis.if_result(mod.results[1], x_both))
# You can also add instructions that will execeute only when the result is zero.
qis.if_result(mod.results[0], zero=lambda: qis.x(mod.qubits[0]))
# In general, you can provide both the one and zero branches at the same time.
qis.if_result(
mod.results[0],
one=lambda: qis.z(mod.qubits[0]),
zero=lambda: qis.y(mod.qubits[0]),
)
if __name__ == "__main__":
print(mod.ir())