Skip to content

Hello Many Worlds

View on GitHub Experiment in the IDE

With Classiq (as, perhaps, with quantum computing) there are many parallel worlds in which to work; specifically, the IDE and Python SDK interfaces. The 'Hello World' example below runs in different flows of the design-optimize-analyze-execute steps 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 the simple arithmetic operation \(y=x^2+1\) in a superposition. This example is covered in depth in Classiq 101.

All in IDE

Copy and paste the following code in the Model tab in the IDE, then click Synthesize as shown in the GIF:

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

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

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

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

That's it! You just completed designing, optimizing, analyzing, and 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

Optimize your algorithm by running this code from the Python SDK:

quantum_program = synthesize(create_model(main))

Analyze your quantum circuit in the IDE by running this code in Python and opening the pop-up link:

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

Execute your code in the Python SDK by running this:

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 in the Python SDK.

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

Now, run the same example using a mixed flow of the previous two:

Design your hello world quantum algorithm by running this 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

Optimize your algorithm by running this code in the Python SDK:

quantum_program = synthesize(create_model(main))

Analyze your quantum circuit in the IDE by running this code in Python and opening the pop-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 two blocks in the quantum circuit: hadamard_transform and Arithmetic, then click Execute:

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

In the Jobs tab, check the results by hovering over the histogram bars and verifying you receive 16 bars. 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 with Classiq 101. Enjoy!

write_qmod(create_model(main), "hello_many_worlds")