pyquil.pyqvm module

class pyquil.pyqvm.AbstractQuantumSimulator(n_qubits: int, rs: numpy.random.mtrand.RandomState)[source]

Bases: abc.ABC


  • n_qubits – Number of qubits to simulate.

  • rs – a RandomState (shared with the owning PyQVM) for doing anything stochastic.

abstract do_gate(gate: pyquil.quilbase.Gate) pyquil.pyqvm.AbstractQuantumSimulator[source]

Perform a gate.


self to support method chaining.

abstract do_gate_matrix(matrix: numpy.ndarray, qubits: Sequence[int]) pyquil.pyqvm.AbstractQuantumSimulator[source]

Apply an arbitrary unitary; not necessarily a named gate.

  • matrix – The unitary matrix to apply. No checks are done

  • qubits – A list of qubits to apply the unitary to.


self to support method chaining.

abstract do_measurement(qubit: int) int[source]

Measure a qubit and collapse the wavefunction


The measurement result. A 1 or a 0.

abstract do_post_gate_noise(noise_type: str, noise_prob: float, qubits: List[int]) pyquil.pyqvm.AbstractQuantumSimulator[source]

Apply noise that happens after each gate application.

WARNING! This is experimental and the signature of this interface will likely change.

  • noise_type – The name of the noise type

  • noise_prob – The probability of that noise happening

  • qubits – Apply noise to these qubits.


self to support method chaining

do_program(program: pyquil.quil.Program) pyquil.pyqvm.AbstractQuantumSimulator[source]

Perform a sequence of gates contained within a program.


program – The program



abstract expectation(operator: Union[pyquil.paulis.PauliTerm, pyquil.paulis.PauliSum]) complex[source]

Compute the expectation of an operator.


operator – The operator


The operator’s expectation value

abstract reset() pyquil.pyqvm.AbstractQuantumSimulator[source]

Reset the wavefunction to the |000...00> state.


self to support method chaining.

abstract sample_bitstrings(n_samples: int) numpy.ndarray[source]

Sample bitstrings from the current state.


n_samples – The number of bitstrings to sample


A numpy array of shape (n_samples, n_qubits)

class pyquil.pyqvm.PyQVM(n_qubits: int, quantum_simulator_type: Optional[Type[pyquil.pyqvm.AbstractQuantumSimulator]] = None, seed: Optional[int] = None, post_gate_noise_probabilities: Optional[Dict[str, float]] = None)[source]

Bases: pyquil.api._qam.QAM[PyQVM]

PyQuil’s built-in Quil virtual machine.

This class implements common control flow and plumbing and dispatches the “actual” work to quantum simulators like ReferenceWavefunctionSimulator, ReferenceDensitySimulator, and NumpyWavefunctionSimulator

  • n_qubits – The number of qubits. Typically this results in the allocation of a large ndarray, so be judicious.

  • quantum_simulator_type – A class that can be instantiated to handle the quantum aspects of this QVM. If not specified, the default will be either NumpyWavefunctionSimulator (no noise) or ReferenceDensitySimulator (noise)

  • post_gate_noise_probabilities – A specification of noise model given by probabilities of certain types of noise. The dictionary keys are from “relaxation”, “dephasing”, “depolarizing”, “phase_flip”, “bit_flip”, and “bitphase_flip”. WARNING: experimental. This interface will likely change.

  • seed – An optional random seed for performing stochastic aspects of the QVM.

execute(executable: Union[pyquil.api._abstract_compiler.EncryptedProgram, pyquil.quil.Program]) pyquil.pyqvm.PyQVM[source]

Execute a program on the PyQVM. Note that the state of the instance is reset on each call to execute.


self to support method chaining.

execute_once(program: pyquil.quil.Program) pyquil.pyqvm.PyQVM[source]

Execute one outer loop of a program on the PyQVM without re-initializing its state.

Note that the PyQVM is stateful. Subsequent calls to execute_once() will not automatically reset the wavefunction or the classical RAM. If this is desired, consider starting your program with RESET.


self to support method chaining.

find_label(label: Union[pyquil.quilatom.Label, pyquil.quilatom.LabelPlaceholder]) int[source]

Helper function that iterates over the program and looks for a JumpTarget that has a Label matching the input label.


label – Label object to search for in program


Program index where label is found

get_result(execute_response: pyquil.pyqvm.PyQVM) pyquil.api._qam.QAMExecutionResult[source]

Return results from the PyQVM according to the common QAM API. Note that while the execute_response is not used, it’s accepted in order to conform to that API; it’s unused because the PyQVM, unlike other QAM’s, is itself stateful.

read_memory(*, region_name: str) numpy.ndarray[source]
transition() bool[source]

Implements a QAM-like transition.

This function assumes program and program_counter instance variables are set appropriately, and that the wavefunction simulator and classical memory ram instance variables are in the desired QAM input state.


whether the QAM should halt after this transition.

write_memory(*, region_name: str, offset: int = 0, value: int = 0) pyquil.pyqvm.PyQVM[source]