Import the IBM setup, the gates, and the compiler engine:
In [1]:
import projectq.setups.ibm # Imports the default compiler to map to IBM QE
from projectq.backends import IBMBackend
from projectq.ops import All, Entangle, Measure
from projectq import MainEngine
Create the compiler using the default compiler engines for the IBM backend and allocate a quantum register of 3 qubits:
In [2]:
engine = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=True, device='ibmqx4'),
engine_list=projectq.setups.ibm.get_engine_list())
qureg = engine.allocate_qureg(3)
Entangle the quantum register:
In [3]:
Entangle | qureg
Measure the quantum register and run the circuit:
In [4]:
All(Measure) | qureg
engine.flush()
Output the measurement result:
In [5]:
print([int(q) for q in qureg])
Create a new compiler with a command printer as a back-end:
In [6]:
from projectq.backends import CommandPrinter
engine2 = MainEngine(CommandPrinter(),
engine_list=projectq.setups.ibm.get_engine_list())
Allocate a quantum register of 5 qubits:
In [7]:
qureg2 = engine2.allocate_qureg(5)
Entangle quantum register (and run the circuit):
A 5-qubit entangle operation requires one H gate and 4 CNOTs if the qubit chip would support CNOTs between arbitrary qubits:
H | Qubit[0]
CX | ( Qubit[0], Qubit[1] )
CX | ( Qubit[0], Qubit[2] )
CX | ( Qubit[0], Qubit[3] )
CX | ( Qubit[0], Qubit[4] )
In [8]:
Entangle | qureg2
engine2.flush()
As IBM's QE chip does not support CNOTs between arbitrary qubits, the compiler automatically changes the abstract entangle circuit to an equivalent circuit which takes into account the hardware constraints of the IBM QE chip. The resulting circuit is shown above as the output of the command printer.
Create a new compiler with our high-performance quantum simulator as a back-end:
Note
Our Simulator can simulate much larger circuits but here we imported the compiler for the IBM QE chip:
import projectq.setups.ibm
and therefore the compiler will only allow circuits which can be mapped to the IBM QE chip. Running larger simulations is easily possible by using the default compiler (see other example codes)
In [9]:
from projectq.backends import Simulator
engine3 = MainEngine(Simulator())
Allocate a 5-qubit quantum register and apply an entangle operations:
In [10]:
qureg3 = engine3.allocate_qureg(5)
Entangle | qureg3
Measure the quantum register and run the circuit:
In [11]:
All(Measure) | qureg3
engine3.flush()
Output the measurement result:
In [12]:
print([int(q) for q in qureg3])
In [ ]: