text: Advanced Usage¶
  First, initialize a localQVM instance on your laptop. You should have two consoles open in your terminal to run in the background.
  ### CONSOLE 1 $ quilc -S port triggered: 6000. [2018-09-19 11:22:37] Starting server: 0.0.0.0 : 6000. ### CONSOLE 2 $ qvm -S Welcome to the Rigetti QVM (Configured with 2048 MiB of workspace and 8 workers.) [2018-09-20 15:39:50] Starting server on port 5000.
  from pyquil import Program, get_qc from pyquil.gates import * qvm = get_qc('9q-square-qvm')
  Now that our local endpoints are up and running, we can start running pyQuil programs! Open a jupyter notebook (type ..code::jupyter notebook in your terminal), or launch python in your terminal (type ..code::python3).
  Using Qubit Placeholders¶
  In PyQuil, we typically use integers to identify qubits
  from pyquil.quil import Program from pyquil.gates import CNOT, H print(Program(H(0), CNOT(0, 1)))
  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
  QubitPlaceholders comes in.
  from pyquil.quilatom import QubitPlaceholder q0 = QubitPlaceholder() q1 = QubitPlaceholder() prog = Program(H(q0), CNOT(q0, q1)) print(prog)
  H {q4402789176} CNOT {q4402789176} {q4402789120}
  If you try to use this program directly, it will not work
  print(prog.out())
  --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-3-da474d3af403> in <module>() ----> 1 print(prog.out()) ... pyquil/pyquil/quilatom.py in out(self) 53 class QubitPlaceholder(QuilAtom): 54 def out(self): ---> 55 raise RuntimeError("Qubit {} has not been assigned an index".format(self)) 56 57 def __str__(self): RuntimeError: Qubit q4402789176 has not been assigned an index
  Instead, you must explicitly map the placeholders to physical qubits. By
  default, the function
  address_qubits will address qubits from 0 to
  N.
  from pyquil.quil import address_qubits print(address_qubits(prog))
  H 0 CNOT 0 1
  The real power comes into play when you provide an explicit mapping
  print(address_qubits(prog, qubit_mapping={ q0: 14, q1: 19, }))
  H 14 CNOT 14 19
  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.
  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)}))
  H 0 H 2 H 4 H 6 H 8 H 10 H 12 H 14 = qvm
  In PyQuil 1.x, there was an object named
  ParametricProgram:
  #
  This object has been removed from PyQuil 2. Please consider simply using a Python function for the above functionality:
  par_p = rotator
  Or using declared classical memory:
  p = Program() angle = p.declare('angle', 'REAL') p += RX(angle, 0)
  exponential_map returns a function allowing you to fill in a multiplicative
  constant later. This commonly occurs in variational algorithms. The function
  exponential_map is used to compute exp[-i * alpha * H] without explicitly filling in a
  value for alpha.
  expH = pl.exponential_map(H) print(expH(0.0)) print(expH(1.0)) print(expH(2.0))