Skip to content

Hello Many Worlds

With Classiq (as perhaps in quantum) there are many parallel worlds one can work with. These refer to the IDE - Python SDK interfaces. In the following we run the same 'Hello World' example in different flows of the steps design-optimize-analyze-execute between the IDE and the Python SDK:

  1. All in IDE
  2. Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (SDK)
  3. Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (IDE)

The 'Hello World' example calculates in a superposition the simple arithmetic operation \(y=x^2+1\) - an example that is covered in depth in the Classiq 101.

All in IDE

Copy and paste the following code in the Model tab in the IDE, then press Synthesize as the GIF belows shows:

qfunc main(output x: qnum, output y:qnum){
  allocate<4>(x);
  hadamard_transform(x);
  y = x**2+1;
}

Check that you receive 2 blocks in the quantum circuit: hadamard_transform and Arithmetic, then press Execute:

In the Execution tab, choose the simulator under the Classiq provider (and make sure that other options are unmarked). Change the job name to be 'hello world' and press Run:

In the Jobs tab check the results by hovering over the histogram bars and verifying you receive 16 bars, and each bar encapsulates the correct relation between \(x\) and \(y\), i.e. \(y=x^2+1\):

That's it! You just completed designing, optimizing, running ans executing your first quantum algorithm with the Classiq IDE!

Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (SDK)

Design your hello world quantum algorithm by running the following code in your favorite Python SDK environment (after installing Classiq):

from classiq import *


@qfunc
def main(x: Output[QNum], y: Output[QNum]):

    allocate(4, x)
    hadamard_transform(x)  # creates a uniform superposition
    y |= x**2 + 1

Then run the following in order to optimize your algorithm from the Python SDK:

quantum_program = synthesize(create_model(main))

Analyze your quantum circuit in the IDE by running the following code in Python and opening the popped up link:

show(quantum_program)
Opening: https://platform.classiq.io/circuit/d6cbc39c-8ab1-4f86-8068-8ec37070673c?version=0.41.0.dev39%2B79c8fd0855

Finally, execute your code from the Python SDK by running the following code:

job = execute(quantum_program)
results = job.result()[0].value.parsed_counts
print(results)
[{'x': 14.0, 'y': 197.0}: 84, {'x': 10.0, 'y': 101.0}: 76, {'x': 2.0, 'y': 5.0}: 74, {'x': 6.0, 'y': 37.0}: 70, {'x': 3.0, 'y': 10.0}: 68, {'x': 7.0, 'y': 50.0}: 66, {'x': 4.0, 'y': 17.0}: 65, {'x': 5.0, 'y': 26.0}: 65, {'x': 0.0, 'y': 1.0}: 64, {'x': 11.0, 'y': 122.0}: 55, {'x': 15.0, 'y': 226.0}: 54, {'x': 12.0, 'y': 145.0}: 54, {'x': 9.0, 'y': 82.0}: 53, {'x': 8.0, 'y': 65.0}: 53, {'x': 1.0, 'y': 2.0}: 53, {'x': 13.0, 'y': 170.0}: 46]

Check that you receive 16 pairs of {'x': , 'y': } and that the values indeed follow the connection \(y=x^2+1\).

That's it! You just completed your first quantum algorithm from the Python SDK!

Design (SDK) - Optimize (SDK) - Analyze (IDE) - Execute (IDE)

Now let's run the same example in a mixed flow of the previous two:

Design your hello world quantum algorithm by running the following code in your favorite Python SDK environment (after installing Classiq):

from classiq import *


@qfunc
def main(x: Output[QNum], y: Output[QNum]):

    allocate(4, x)
    hadamard_transform(x)  # creates a uniform superposition
    y |= x**2 + 1

Then run the following in order to optimize your algorithm from the Python SDK:

quantum_program = synthesize(create_model(main))

Analyze your quantum circuit in the IDE by running the following code in Python and opening the popped up link:

show(quantum_program)
Opening: https://platform.classiq.io/circuit/b24ddce6-083a-442c-8640-addf3aa354c7?version=0.41.0.dev39%2B79c8fd0855

Check that you receive 2 blocks in the quantum circuit: hadamard_transform and Arithmetic, then press Execute:

In the Execution tab, choose the simulator under the Classiq provider (and make sure that other options are unmarked). Change the job name to be 'hello world' and press Run:

In the Jobs tab check the results by hovering over the histogram bars and verifying you receive 16 bars, and each bar encapsulates the correct relation between \(x\) and \(y\), i.e. \(y=x^2+1\):

That's it! You just completed the hello world example in a mixed flow of IDE and Python SDK!

You are ready to continue to the Classiq 101, enjoy!

write_qmod(create_model(main), "hello_many_worlds")