Let's start
Writing and running your own algorithm on our platform is quite straightforward. Even without any registration you can start programming by clicking Try the editor from our homepage.
This will bring you to our editor, where a very simple example program is loaded. You can run the code on our emulator and examine the results. You could also start writing your own code from this location.
Note: As an unregistered user you are limited to a maximum of 5 qubits. When your session is terminated (e.g. when you close your browser) your code will be automatically deleted. You need to register in order to make use of the full functionality of the system.
Examine the code
The algorithm shows how to entangle two qubits using the Hadamard gate and CNOT gate. Under the advanced topics you will find more elaborate code examples.
version 1.0
# number of qubits for this backend is limited to 5 Qubits
qubits 2
{prep_z q[0] | prep_z q[1]}
# Create a superposition state for qubit 0
H q[0]
# Entangle both qubits using a CNOT gate
CNOT q[0], q[1]
The algorithm is written in cQASM. When you are writing more complex algorithms, you need to understand the details and syntax of cQASM.
The file starts with the specification of the cQASM version on line 1. This line should always be present.
Line 3 is a comment. Comments start with a hash (#
) and it and everything following it is ignored by the cQASM parser. Use comments to document your code, so that someone else can understand what you were trying to do (note that that someone else could also be your future self).
Line 4 defines the size of the qubit register. In this simple example, two qubits are used. Each qubit in the register is identified by its index. The first qubit has index and the second qubit has index . Make sure your qubit register does not define more qubits than used by your algorithm. The memory allocated for your algorithm is determined by this number and execution time is optimal when these numbers match.
In line 6 we initialize both qubits in the ground state .
- Note 1: We could have skipped this step because all qubits are initialized in the ground state by definition
- Note 2: We use a pipeline to enforce a parallel execution of these two instructions. The cQASM syntax supports various methods for command parallelization and using ranges and lists for qubit addressing.
In line 9 we perform a Hadamard gate on qubit 0. The Hadamard gate is a single-qubit operation that brings the basis state to .
In line 12 we use a two-qubit operation to entangle both qubits. The CNOT operation flips the second qubit if the first qubit is in state and it does nothing on the second qubit if the first qubit is in state. Since the first qubit is in a superposition state, it both flips and doesn't flip the second qubit, bringing the full system in the entangled state which is one of the Bell states.
Important note: Ending an algorithm with a measurement instruction is usually not required when you run the algorithm on a emulator. In a simulation we can, in most cases, determine the final full quantum state of the system and then sample from all possible outcomes. Algorithms for which this is possible are called deterministic algorithms. In some special cases the final quantum state is non-deterministic. In those cases, you must add a measurement statement at the end of the algorithm in order to get meaningful results. If you do include measurement instructions in your algorithm, the runtime of your algorithm will increase because multiple executions will have to be executed in order to get a histogram of all possible final states. More on this in the paragraph on optimization of simulations.
Visualization of the algorithm
The corresponding circuit diagram is shown beneath the editor pane. Colors are used both in the code and in the circuit visualization to quickly distinguish between initialization and measurement operations, qubit operations and comments.
q[0] | |||||||
---|---|---|---|---|---|---|---|
q[1] |
The circuit is automatically and real-time rendered from the code. The editor also automatically checks your code for obvious errors such as syntax errors, multiple operations on the same qubit at the same time, out-of-range errors etc.
Next to the editor (below the editor if you are working on mobile devices) you will find a list of supported cQASM operations with a short explanation and definition of each operation. More information about a specific gate can be found in our knowledge base of gate operations or by clicking on the learn more link in the operations list.
Running the algorithm
Now it's time to execute this algorithm on the Quantum Inspire emulator and examine the results.
When you are a novice in quantum algorithms, results may sometimes surprise you. In this example, the qubits are in an entangled state. You could almost say that, just before the measurement, both qubits are in state OR both qubits are in state (although this is a simplified explanation of the more complex physical reality).
At the top right you find the RUN
button to initiate the execution of the algorithm. You will see the backend used to run the algorithm on (as an anonymous user, only our QX Single-node Emulator can be used) and the name that will be given to your results files (this name can be changed before you start the execution). You can also change the number of shots, which is the number of times the algorithm will be executed. In this case, because we didn't include any measurement instructions, the shots will be executed as virtual shots. This is referred to as simulation optimization. We can choose 16 shots to begin with.
When you execute the algorithm, it will be scheduled in a queue. The waiting time depends on the length of the queue, your position in the queue and the time needed to run the algorithm. When your algorithm is executed, all shots will be executed directly after one another.
While your algorithm is scheduled to run, you can return to the editor, change the algorithm and execute another run.
Examining the results
Let's take a look at what our original algorithm produced.
If you click on Results
a list of the scheduled, running and finished jobs is shown, sorted from newest to oldest. By clicking on Editor
, you can always return to the editor. By now, the algorithm has probably finished. The status of each job shown on the screen can be updated by refreshing your browser (e.g. clicking F5 on most browsers).
When you open one of the results a histogram will be shown looking like this, although the exact values will probably be different:
When we measure a qubit, the result can be a 0 or a 1. In quantum inspire, the results of qubit measurements are stored in the binary register. What the histogram shows is not the final state of the qubits, but what the contents of the binary register would be if both qubits would have been measured.
The chart shows a histogram over all shots of the state of the binary register at the end of the algorithm. In the example histogram above, the binary register was 00 for 25% of the shots, and 11 for 75% of the shots. The rightmost bit is the measurement value of the measured qubit with the lowest index (in this case q[0]). The leftmost bit is the measurement value of the measured qubit with the highest index (in this case q[1]).
The results show, that whenever the result of the measurement on q[0] was 0, the result of the measurement on q[1] was also 0. And, whenever the result of the measurement on q[0] was 1, the result of the measurement on q[1] was also 1, exactly what we expected.
By clicking Run again
we can execute another run of the algorithm, which could give a result like this:
Besides the histogram data of the binary register, the results pane also shows the back-end that was used for the simulation, the number of shots and the optimization method used for the simulation.
Also provided are links to download different kinds of results files, giving you access to the qubits states or binary register during your algorithm.
This is all the basic information you need to write and run your own algorithms! Go ahead and enjoy!
Ready for more? Our Quick guide gives you more information on the editor, managing your account and managing your projects. Expert users can go to our list of Advanced topics to find more info on our SDK, how to create more complex algorithms, optimize quantum circuits etc.