Skip to content

Arithmetic Expressions

View on GitHub Experiment in the IDE

Use the Arithmetic function to write complex mathematical expression in free format. The notation follows the Python language for math notation.

The function first parses the expression and builds an abstract syntax tree (AST). Then, the Classiq engine finds a computation strategy for a specified number of qubits, and compiles the desired quantum program.

As opposed to classical computers, when quantum computers evaluate arithmetic expression, the calculations are reversible and are applied on all quantum states in parallel. To do so, quantum computers store all intermediate computation results in a quantum variables. Qubits that are not freed cannot be used later on in the quantum program.

Analogously to the classical world, there is a form of quantum "garbage collection", usually referred to as uncomputation, which returns the garbage qubits to their original state. The computation strategy determines the order in which qubits are released and reused. By employing different strategies, you can produce a variety of quantum programs with the same functionality. In general, longer quantum programs require less qubits than shorter ones.

Supported operators:

  • Add: +

  • Subtract: - (two arguments)

  • Negate: - (a single argument)

  • Multiply: *

  • Bitwise Or: |

  • Bitwise And: &

  • Bitwise Xor: ^

  • Invert: ~

  • Equal: ==

  • Not Equal: !=

  • Greater Than: >

  • Greater Or Equal: >=

  • Less Than: <

  • Less Or Equal: <=

  • Modulo: % limited for power of 2

  • Logical And: and

  • Logical Or: or

  • Max: max (n>=2 arguments)

  • Min: min (n>=2 arguments)

  • Power ** (register base, positive int power)

Example

This example generates a quantum program that calculates the expression (a + b + c & 15) % 8 ^ 3 & a ^ 10 == 4. Each of the variables a,b, and c is defined as a quantum varialbe with a different size.

from classiq import *


@qfunc
def main(a: Output[QNum], b: Output[QNum], c: Output[QNum], res: Output[QNum]) -> None:
    prepare_int(2, a)
    prepare_int(1, b)
    prepare_int(5, c)

    res |= (a + b + c & 15) % 8 ^ 3 & a ^ 10 == 4


qmod = create_model(main, out_file="arithmetic_expression_example")
qprog = synthesize(qmod)

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