Skip to content

Your first Classiq model

Here we will create your first Classiq model. We will create a Quantum program that will perfrom an addition between an integer and superpostion of integers.

# !pip install -U classiq
from classiq import *

# If you have not yet authenticated please uncomment the line below
# authenticate()
# QFunc declares a quantum function
@qfunc
# The main quantum function that will produce 3 outputs.
def main(res: Output[QNum], a: Output[QNum], b: Output[QNum]):
    # Encode the integer value 3 in variable a
    prepare_int(3, a)

    # Encode the super position of 0 and 3 into variable b
    prepare_state([0.5, 0, 0, 0.5], out=b, bound=0.01)

    # Add a and b togehter, and save the result in res
    res |= a + b


# Create a quantum circuit from the model above
qprog = synthesize(create_model(main))

# Open the circuit in the IDE, where it can be analyzed and executed
show(qprog)
Opening: https://platform.classiq.io/circuit/b39feba2-d22e-4d2e-a5a7-03c097d59ed5?version=0.41.0.dev39%2B79c8fd0855

Execution time

Now that we have created a model, it is time to execute it. With the show() command the IDE is opened where we can execute the circuit. A page like this should have opened:

Click on the execute button on the right top. Next, you will get to the execution page where you can run the circuit, validate that you are running on the Classiq Aer simulation as highlighted in the image below, and press the Run button as seen below.

This should be the result of that circuit:

As you can see, there are two answers found to the equation res = a + b.

  1. 3 + 0 = 3
  2. 3 + 3 = 6

So, our first quantum algorithm has done exactly what we wanted to do.

Do it yourself

Initialize the input variables in these ways:

  • a: A superposition with an equal possibility to get a 0, 2 or 3.
  • b: 25% chance of 0 and 75% of 3
  • c: 50/50 chance of a 0 or 1

The result variable should hold the result of (a * b) + c

@qfunc
def main(
    # Uncomment the variables below
    # a: Output[QNum],
    # b: Output[QNum],
    # c: Output[QNum],
    # res:Output[QNum]
):
    # Your code here
    pass


qprog = synthesize(create_model(main))
show(qprog)
Opening: https://platform.classiq.io/circuit/d5a3e1a4-0178-404e-8ac2-96c703c54111?version=0.41.0.dev39%2B79c8fd0855

The full solution for your reference

@qfunc
def main(a: Output[QNum], b: Output[QNum], c: Output[QNum], res: Output[QNum]):
    prepare_state([1 / 3, 0, 1 / 3, 1 / 3], out=a, bound=0.1)
    prepare_state([1 / 4, 0, 0, 3 / 4], out=b, bound=0.1)
    prepare_state([1 / 2, 1 / 2], out=c, bound=0.1)

    res |= (a * b) + c


qprog = synthesize(create_model(main))
show(qprog)
Opening: https://platform.classiq.io/circuit/cf2e3aa9-5411-4fea-9bb5-22860475f486?version=0.41.0.dev39%2B79c8fd0855