Minimum and Maximum¶
The minimum and maximum operators determine the smallest and largest input, respectively. Both functions receive two inputs. Each may be a fixed point number or a quantum register.
In [1]:
Copied!
from classiq import (
Output,
QArray,
QBit,
QNum,
allocate,
create_model,
hadamard_transform,
prepare_int,
qfunc,
)
from classiq.qmod.symbolic import min
@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
prepare_int(4, a)
allocate(3, b)
hadamard_transform(b)
res |= min(a, b)
qmod = create_model(main)
from classiq import (
Output,
QArray,
QBit,
QNum,
allocate,
create_model,
hadamard_transform,
prepare_int,
qfunc,
)
from classiq.qmod.symbolic import min
@qfunc
def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:
prepare_int(4, a)
allocate(3, b)
hadamard_transform(b)
res |= min(a, b)
qmod = create_model(main)
In [2]:
Copied!
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "minimum_2vars_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "minimum_2vars_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
Out[2]:
[{'a': 4.0, 'b': 6.0, 'res': 4.0}: 146, {'a': 4.0, 'b': 0.0, 'res': 0.0}: 142, {'a': 4.0, 'b': 3.0, 'res': 3.0}: 137, {'a': 4.0, 'b': 1.0, 'res': 1.0}: 132, {'a': 4.0, 'b': 7.0, 'res': 4.0}: 116, {'a': 4.0, 'b': 4.0, 'res': 4.0}: 113, {'a': 4.0, 'b': 5.0, 'res': 4.0}: 108, {'a': 4.0, 'b': 2.0, 'res': 2.0}: 106]
Example 2: Float and Quantum Variable Maximum¶
This code example returns a quantum program with a maximum of two arguments. Here, the left arg is a fixed-point number $(11.1)_2$ (3.5), and the right arg is a quantum variable of size three.
In [3]:
Copied!
from classiq import (
Output,
QArray,
QBit,
QNum,
allocate,
create_model,
hadamard_transform,
qfunc,
)
from classiq.qmod.symbolic import max
@qfunc
def main(a: Output[QNum], res: Output[QNum]) -> None:
allocate(3, a)
hadamard_transform(a)
res |= max(3.5, a)
qmod = create_model(main)
from classiq import (
Output,
QArray,
QBit,
QNum,
allocate,
create_model,
hadamard_transform,
qfunc,
)
from classiq.qmod.symbolic import max
@qfunc
def main(a: Output[QNum], res: Output[QNum]) -> None:
allocate(3, a)
hadamard_transform(a)
res |= max(3.5, a)
qmod = create_model(main)
In [4]:
Copied!
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "maximum_float_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
from classiq import execute, synthesize, write_qmod
write_qmod(qmod, "maximum_float_example")
qprog = synthesize(qmod)
result = execute(qprog).result()[0].value
result.parsed_counts
Out[4]:
[{'a': 3.0, 'res': 3.5}: 144, {'a': 5.0, 'res': 5.0}: 141, {'a': 2.0, 'res': 3.5}: 134, {'a': 4.0, 'res': 4.0}: 127, {'a': 7.0, 'res': 7.0}: 121, {'a': 6.0, 'res': 6.0}: 118, {'a': 1.0, 'res': 3.5}: 116, {'a': 0.0, 'res': 3.5}: 99]