<jupyter_start><jupyter_text>ProjectQ Demo Compiling code for IBM QE Import the IBM setup, the gates, and the compiler engine:<jupyter_code>import projectq.setups.ibm # Imports the default compiler to map to IBM QE
from projectq.backends import IBMBackend
from projectq.ops import Measure, Entangle
from projectq import MainEngine<jupyter_output><empty_output><jupyter_text>Create the compiler using the default compiler engines for the IBM backend and allocate a quantum register of 3 qubits:<jupyter_code>engine = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=True, device='ibmqx4'))
qureg = engine.allocate_qureg(3)<jupyter_output><empty_output><jupyter_text>Entangle the quantum register:<jupyter_code>Entangle | qureg<jupyter_output><empty_output><jupyter_text>Measure the quantum register and run the circuit:<jupyter_code>Measure | qureg
engine.flush()<jupyter_output>Authenticating...
IBM QE user (e-mail) > <EMAIL>
IBM QE password > ········
Running code...
Waiting for results...
Done.
00000 with p = 0.4814453125
10000 with p = 0.00390625
01000 with p = 0.0087890625
11000 with p = 0.0205078125
00100 with p = 0.02734375
10100 with p = 0.046875
01100 with p = 0.04296875
11100 with p = 0.3681640625*<jupyter_text>Output the measurement result:<jupyter_code>print([int(q) for q in qureg])<jupyter_output>[1, 1, 1]<jupyter_text>Inspecting the gates required for a 5-qubit entangle on IBM QE Create a new compiler with a command printer as a back-end:<jupyter_code>from projectq.backends import CommandPrinter
engine2 = MainEngine(CommandPrinter())<jupyter_output><empty_output><jupyter_text>Allocate a quantum register of 5 qubits:<jupyter_code>qureg2 = engine2.allocate_qureg(5)<jupyter_output><empty_output><jupyter_text>Entangle quantum register (and run the circuit):A 5-qubit entangle operation requires one H gate and 4 CNOTsif 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] )```<jupyter_code>Entangle | qureg2
engine2.flush()<jupyter_output>Allocate | Qureg[0]
Allocate | Qureg[1]
H | Qureg[1]
CX | ( Qureg[1], Qureg[0] )
Allocate | Qureg[2]
H | Qureg[2]
CX | ( Qureg[2], Qureg[0] )
Allocate | Qureg[3]
H | Qureg[3]
CX | ( Qureg[3], Qureg[0] )
Allocate | Qureg[4]
H | Qureg[4]
CX | ( Qureg[4], Qureg[0] )
H | Qureg[0]
H | Qureg[1]
H | Qureg[2]
H | Qureg[3]
H | Qureg[4]<jupyter_text>As IBM's QE chip does not support CNOTs between arbitrary qubits, the compiler automaticallychanges the abstract entangle circuit to an equivalent circuit which takesinto account the hardware constraints of the IBM QE chip. The resulting circuit is shown above as the output of the command printer.  Simulating a circuit for the IBM QE chip 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)<jupyter_code>from projectq.backends import Simulator
engine3 = MainEngine(Simulator())<jupyter_output><empty_output><jupyter_text>Allocate a 5-qubit quantum register and apply an entangle operations:<jupyter_code>qureg3 = engine3.allocate_qureg(5)
Entangle | qureg3<jupyter_output><empty_output><jupyter_text>Measure the quantum register and run the circuit:<jupyter_code>Measure | qureg3
engine3.flush()<jupyter_output><empty_output><jupyter_text>Output the measurement result:<jupyter_code>print([int(q) for q in qureg3])<jupyter_output>[1, 1, 1, 1, 1]
StarcoderdataJupyterStructuredCleanDedup