-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
logic.py
27 lines (21 loc) · 797 Bytes
/
logic.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
from qiskit import QuantumCircuit
from qiskit.circuit.classicalfunction.classicalfunction import ClassicalFunction
def oracle(logic, n):
'''
Returns a quantum circuit that implementes the logic for n qubits.
Parameters:
logic: a Python function using the format below.
def oracle_func(x1: Int1, x2: Int1, x3: Int1) -> Int1:\n return (x1 and not x2 and not x3)
n: the number of qubits in the circuit.
'''
# Convert the logic to a quantum circuit.
formula = ClassicalFunction(logic)
fc = formula.synth()
# Convert the quantum circuit to a quantum program.
qc = QuantumCircuit(n+1)
qc.compose(fc, inplace=True)
print(qc.draw())
# Convert the oracle to a gate.
gate = qc.to_gate()
gate.name = "oracle"
return gate