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).
a | b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
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 register size mismatch.
For example, the positive signed number \((110)_2=6\) is expressed as \((00110)_2\) when operating with a five-qubit register. Similarly, the negative signed number \((110)_2=-2\) is expressed as \((11110)_2\).
Examples:
5 & 3 = 1 since 101 & 011 = 001
5 & -3 = 5 since 0101 & 1101 = 0101
-5 & -3 = -7 since 1011 & 1101 = 1001
Syntax¶
Function: BitwiseAnd
Parameters:
left_arg: Union[int, RegisterUserInput]
(see RegisterUserInput)right_arg: Union[int, RegisterUserInput]
(see RegisterUserInput)output_size: Optional[PositiveInt]
Register names:
left_arg
:left_arg
right_arg
:right_arg
result
:bitwise_and
{
"function": "BitwiseAnd",
"function_params": {
"left_arg": 3,
"right_arg": { "size": 3 }
}
}
Example 1: Two Register¶
{
"functions": [
{
"name": "main",
"body": [
{
"function": "BitwiseAnd",
"function_params": {
"left_arg": {"size": 5, "is_signed":true},
"right_arg": {"size": 3}
}
}
]
}
]
}
from classiq import Model, RegisterUserInput, synthesize
from classiq.builtin_functions import BitwiseAnd
params = BitwiseAnd(
left_arg=RegisterUserInput(size=5, is_signed=True),
right_arg=RegisterUserInput(size=3),
)
model = Model()
model.BitwiseAnd(params)
quantum_program = synthesize(model.get_model())
This example generates a circuit that performs a bitwise 'and' between two registers. The left arg is signed with five qubits and the right arg is unsigned with three qubits.
Example 2: Integer and Register¶
{
"functions": [
{
"name": "main",
"body": [
{
"function": "BitwiseAnd",
"function_params": {
"left_arg": 3,
"right_arg": {"size": 3}
}
}
]
}
]
}
from classiq import Model, RegisterUserInput, synthesize
from classiq.builtin_functions import BitwiseAnd
params = BitwiseAnd(left_arg=3, right_arg=RegisterUserInput(size=3))
model = Model()
model.BitwiseAnd(params)
quantum_program = synthesize(model.get_model())
This example generates a circuit that performs a bitwise 'and' between a quantum register and an integer. The left arg is an integer equal to three and the right arg is an unsigned quantum register with three qubits.