#!/usr/bin/env python # coding: utf-8 # # Writing a basic quantum program # # In this notebook, you will learn how to write a quantum program in Python on the Quantum Learning Machine. # # # In the QLM python framework, a quantum program is contained in a ``Program`` class. It comes with methods to allocate quantum and classical registers, apply gates, measures and resets. A given instance of a ``Program`` can then be converted to a quantum ``Circuit``, which is the object that can be fed to a quantum processor. # # ### Initializaton of a Quantum Program # # In the following snippet, we instantiate a quantum program object called ``prog``: # In[ ]: from qat.lang.AQASM import Program prog = Program() # ### Quantum bit allocation # # The allocation of quantum bits is done via the ``qalloc`` method. It returns a register, which will then be used to refer to the qubits. # # In the following snippet, we allocated an 8-qubit register: # In[ ]: qbits = prog.qalloc(8) # ### Applying quantum gates # # The application of quantum gates is carried out through the ``apply`` method. # # Here, we apply standard Hadamard gate on qbit 0, a Pauli X gate on qubit 1, a CNOT gate on qubits 1 and 5, and a phase gate with angle $\pi/6$ to qubit 5. # In[ ]: from qat.lang.AQASM import H, X, CNOT, PH from math import pi prog.apply(H, qbits[0]) prog.apply(X, qbits[1]) prog.apply(CNOT, qbits[1], qbits[5]) prog.apply(PH(pi/6), qbits[5]) # ### Generation of a quantum circuit # # Before we describe other common operations such as measurements, let us introduce the final step that allows to generate a quantum-simulation-ready quantum circuit out of the quantum program. # # This generation is done via the ``to_circ`` method: # In[ ]: circ = prog.to_circ() # ### Circuit display # The ``.display`` method outputs a graphical representation of the quantum circuit: # In[ ]: circ.display() # For a more comprehensive reference on the Python AQASM library, including **measures, classical control, custom gates, etc.**, check out [this tutorial](py_aqasm.ipynb). # You will find a list of all available gates [here](available_gates.ipynb). # In[ ]: