Skip to content

Minimum and Maximum

View on GitHub

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.

Examples

Example 1: Two Quantum Variables Minimum

This code example generates a quantum program that returns a minimum of two arguments. Both the left and right arguments are defined as quantum variables of size three.

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 execute, synthesize, write_qmod

write_qmod(qmod, "minimum_2vars_example")
qprog = synthesize(qmod)

result = execute(qprog).result()[0].value
result.parsed_counts
[{'a': 4.0, 'b': 1.0, 'res': 1.0}: 136,
 {'a': 4.0, 'b': 7.0, 'res': 4.0}: 129,
 {'a': 4.0, 'b': 6.0, 'res': 4.0}: 126,
 {'a': 4.0, 'b': 4.0, 'res': 4.0}: 125,
 {'a': 4.0, 'b': 2.0, 'res': 2.0}: 122,
 {'a': 4.0, 'b': 3.0, 'res': 3.0}: 122,
 {'a': 4.0, 'b': 5.0, 'res': 4.0}: 122,
 {'a': 4.0, 'b': 0.0, 'res': 0.0}: 118]

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.

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 execute, synthesize, write_qmod

write_qmod(qmod, "maximum_float_example")
qprog = synthesize(qmod)

result = execute(qprog).result()[0].value
result.parsed_counts
[{'a': 5.0, 'res': 5.0}: 135,
 {'a': 0.0, 'res': 3.5}: 131,
 {'a': 3.0, 'res': 3.5}: 130,
 {'a': 6.0, 'res': 6.0}: 129,
 {'a': 2.0, 'res': 3.5}: 127,
 {'a': 7.0, 'res': 7.0}: 122,
 {'a': 1.0, 'res': 3.5}: 115,
 {'a': 4.0, 'res': 4.0}: 111]