#!/usr/bin/env python # coding: utf-8 # # Using `QubitPlaceholder`s # In PyQuil, we typically use integers to identify qubits # In[1]: from pyquil import Program from pyquil.gates import CNOT, H print(Program(H(0), CNOT(0, 1))) # However, when running on real, near-term QPUs we care about what particular physical qubits our program will run on. In fact, we may want to run the same program on an assortment of different qubits. This is where using `QubitPlaceholder`s comes in. # In[2]: from pyquil.quilatom import QubitPlaceholder q0 = QubitPlaceholder() q1 = QubitPlaceholder() prog = Program(H(q0), CNOT(q0, q1)) print(prog) # If you try to use this program directly, it will not work # In[3]: try: print(prog.out()) except RuntimeError as e: print(e) # Instead, you must explicitly map the placeholders to physical qubits. By default, the function `address_qubits` will address qubits from 0 to N. # In[4]: from pyquil.quil import address_qubits print(address_qubits(prog)) # The real power comes into play when you provide an explicit mapping # In[5]: print(address_qubits(prog, qubit_mapping={ q0: 14, q1: 19, })) # ## Register # Usually, your algorithm will use an assortment of qubits. You can use the convenience function `QubitPlaceholder.register()` to request a list of qubits to build your program. # In[6]: qbyte = QubitPlaceholder.register(8) prog2 = Program(H(q) for q in qbyte) print(address_qubits(prog2, {q: i*2 for i, q in enumerate(qbyte)}))