In this post I cover how to use a constant boolean value in a logical expression for Qiskit PhaseOracle
.
TL;DR BitVec(1, '1')
for True and BitVec(1, '0')
for False
At the time of writing this post, Qiskit library includes PhaseOracle
class that constructs quantum circuits for arbitrary provided logical expression. Consider a simple example:
oracle = PhaseOracle("(a | b) | c")
oracle.draw()
Output:
q_0: ─■─────────
│
q_1: ─o───────■─
│ ┌───┐ │
q_2: ─o─┤ Z ├─o─
└───┘
For some applications you might want to use a constant true or false boolean value in the provided boolean expression. I stumbled upon this need myself, trying to implement a paper on distributed Grover’s algorithm (hopefully, I will also write a post about it). In the algorithm, we needed a step to transform an n-qubit oracle into two (n-1)-qubit oracles. For this, we wanted to replace some of the variables in the expression to a constant boolean. To get 2 oracles from the original we would replace all appearances of the variable in the original logical expression to True for the first oracle, and all appearances to False for the second one.
Qiskit documentation doesn’t mention functionality of constant boolean variables in these expression. As logical expression parsing and calculation is taken care of by another package, I could see how that can be a valid approach in the documentation. The problem is that the original library documentation doesn’t mention constant either, despite having this functionality in code. Here it is – you can use BitVec(1, '0')
to use as False and BitVec(1, '1')
as True. You can clearly see it in example below.
Let’s replace variable c
from the original example with the false value:
oracle = PhaseOracle("(a | b) | BitVec(1, '0')")
oracle.draw()
Output:
q_0: ─■──────
│ ┌───┐
q_1: ─o─┤ Z ├
└───┘
The output circuit is exactly the same as the one for an equivalent expression (a | b)
:
oracle = PhaseOracle("a | b")
oracle.draw()
Output:
q_0: ─■──────
│ ┌───┐
q_1: ─o─┤ Z ├
└───┘
And the same can be done for the true value:
oracle = PhaseOracle("(a | b) & BitVec(1, '1')")
oracle.draw()
Output:
q_0: ─■──────
│ ┌───┐
q_1: ─o─┤ Z ├
└───┘
Stay tuned for more posts about quantum computing!
I don’t ask for donations - just visit my about page.