Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Bitwise And

The Bitwise And (denoted as ’&’) is implemented by applying this truth table between each pair of qubits in register A and B (or qubit and bit). 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 register size mismatch. For example, the positive signed number (110)2=6(110)_2=6 is expressed as (00110)2(00110)_2 when operating with a five-qubit register. Similarly, the negative signed number (110)2=2(110)_2=-2 is expressed as (11110)2(11110)_2. Examples: 5 & 3 = 1 since 101 & 011 = 001 5 & -3 = 5 since 0101 & 1101 = 0101 -5 & -3 = -7 since 1011 & 1101 = 1001

Examples

Example 1: Two Quantum Variables

This example generates a quantum program that performs a bitwise ‘and’ between two variables, both are unsigned integer
from classiq import *


@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
    a |= 4
    b |= 5
    res |= a & b


qmod = create_model(main)

qprog = synthesize(qmod)

result = execute(qprog).result_value()
result.parsed_counts
Output:
[{'a': 4.0, 'b': 5.0, 'res': 4.0}: 1000]
  

Example 2: Integer and Quantum Variable

This example generates a quantum program that performs a bitwise ‘and’ between a quantum variable and an integer. The left arg is an integer equal to 3 and the right arg is an unsigned quantum variable with three qubits.
@qfunc
def main(a: Output[QNum], res: Output[QNum]) -> None:
    a |= 5
    res |= 3 & a


qmod = create_model(main)

qprog = synthesize(qmod)

result = execute(qprog).result_value()
result.parsed_counts
Output:
[{'a': 5.0, 'res': 1.0}: 1000]