Skip to content

Walk-through: prepare_state

This notebook is the Classiq SDK equivalent of the walk-through sequence as presented in the Classiq web IDE [1]

from classiq import (
    Output,
    QArray,
    QBit,
    control,
    create_model,
    execute,
    prepare_state,
    qfunc,
    show,
    synthesize,
)

1/8 Building your algorithm

On the IDE: Click on the Model tab to start writing your quantum model.

2/8 Build the model

On the IDE: Here you define the model, function parameters, and more. See the User Guide [2] for details.

Below is the SDK representation of the qmod syntax shown on the IDE page:

probabilities = [
    0,
    0.002,
    0.004,
    0.006,
    0.0081,
    0.0101,
    0.0121,
    0.0141,
    0.0161,
    0.0181,
    0.0202,
    0.0222,
    0.0242,
    0.0262,
    0.0282,
    0.0302,
    0.0323,
    0.0343,
    0.0363,
    0.0383,
    0.0403,
    0.0423,
    0.0444,
    0.0464,
    0.0484,
    0.0504,
    0.0524,
    0.0544,
    0.0565,
    0.0585,
    0.0605,
    0.0625,
]


@qfunc
def main(io: Output[QArray[QBit]]):
    prepare_state(probabilities=probabilities, bound=0.01, out=io)


model = create_model(main)

We can dump the model to file:

from classiq import write_qmod

write_qmod(model, "prepare_state")

3/8 Synthesize the model

On the IDE: Now that you have selected or built a model, click the "Synthesize" button, sit back, and let Classiq do its magic!

Below is the SDK representation of the qmod syntax shown on the IDE page:

qprog = synthesize(model)
show(qprog)
Opening: https://platform.classiq.io/circuit/ab291813-78a4-4491-9d45-ebbbff1388e2?version=0.41.0.dev39%2B79c8fd0855

4/8 Congratulations!

On the IDE: This is your first quantum program! Learn more in the User Guide [2].

5/8 Run your synthesized quantum program on quantum hardware or simulators

On the IDE: Press the 'Execute' button to define the quantum hardware or a quantum simulator to run your quantum program.

6/8 Execution details

On the IDE: Select which quantum program to execute, define execution parameters and choose a quantum provider and backend platform. The Classiq Platform is your gateway to all major quantum computing providers

from classiq.execution import (
    ClassiqBackendPreferences,
    ClassiqSimulatorBackendNames,
    ExecutionPreferences,
    set_quantum_program_execution_preferences,
)

preferences = ExecutionPreferences(
    backend_preferences=ClassiqBackendPreferences(
        backend_name=ClassiqSimulatorBackendNames.SIMULATOR
    )
)
qprog = set_quantum_program_execution_preferences(qprog, preferences)

7/8 Let's run your quantum program on a quantum simulator!

On the IDE: Press 'Run' button to execute your quantum program on the simulator which we chose in the previous step

Bellow is the simple SDK execute code:

job = execute(qprog)
results = job.result()
job.open_in_ide()

8/8 Wow! Look at that cool triangle probability function!

On the IDE: That's it! You ran your first quantum program.

To learn more about the Classiq Platform visit our User Guide [2].

References

[1]: Classiq IDE.

[2]: Classiq User_Guide.