Bitwise Or
The Bitwise Or (denoted as '|') is implemented by applying the following truth table between each pair of qubits (or qubit and bit) in variables A and B.
a | b | a or b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Note that integer and fixed-point numbers are represented in a two-complement method during function evaluation. The binary number is extended in the case of a variable size mismatch.
For example, the positive signed number \((110)_2=6\) is expressed as \((00110)_2\) when operating with a five-qubit variable. Similarly, the negative signed number \((110)_2=-2\) is expressed as \((11110)_2\).
Examples:
5 | 3 = 7 since 101 | 011 = 111
5 | -3 = -3 since 0101 | 1101 = 1101
-5 | -3 = -1 since 1011 | 1101 = 1111
Examples
Example 1: Two Quantum Variables
This example generates a quantum program that performs bitwise 'or' between two variables. The left arg is a signed with five qubits and the right arg is unsigned with three qubits.
from classiq import (
Output,
QArray,
QBit,
QNum,
allocate_num,
create_model,
inplace_prepare_int,
qfunc,
)
@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
allocate_num(5, True, 0, a)
allocate_num(3, False, 0, b)
inplace_prepare_int(4, a)
inplace_prepare_int(5, b)
res |= a | b
qmod = create_model(main)
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "bitwise_or_2vars_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
print(result.parsed_counts)
print(result.counts_of_multiple_outputs(["a", "b", "res"]))
[{'a': 4.0, 'b': 5.0, 'res': 5.0}: 1000]
{('00100', '101', '10100'): 1000}
Example 2: Integer and Quantum Variable
This example generates a quantum program that performs a bitwise 'or' between a quantum variable and an integer. The left arg is an integer equal to three and the right arg is an unsigned quantum variable with three qubits.
from classiq import Output, QArray, QBit, QNum, create_model, prepare_int, qfunc
@qfunc
def main(a: Output[QNum], res: Output[QNum]) -> None:
prepare_int(4, a)
res |= 3 | a
qmod = create_model(main)
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "bitwise_or_integer_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
[{'a': 4.0, 'res': 7.0}: 1000]