pyquil.wavefunction module

Module containing the Wavefunction object and methods for working with wavefunctions.

class pyquil.wavefunction.Wavefunction(amplitude_vector: ndarray)[source]

Bases: object

Encapsulate a wavefunction representing a quantum state as returned by WavefunctionSimulator.

Note

The elements of the wavefunction are ordered by bitstring. E.g., for two qubits the order is 00, 01, 10, 11, where the the bits are ordered in reverse by the qubit index, i.e., for qubits 0 and 1 the bitstring 01 indicates that qubit 0 is in the state 1. See also the related documentation section in the WavefunctionSimulator Overview.

Initializes a wavefunction

Parameters:

amplitude_vector – A numpy array of complex amplitudes

static from_bit_packed_string(coef_string: bytes) Wavefunction[source]

From a bit packed string, unpacks to get the wavefunction :param coef_string:

get_outcome_probs() Dict[str, float][source]

Parses a wavefunction (array of complex amplitudes) and returns a dictionary of outcomes and associated probabilities.

Returns:

A dict with outcomes as keys and probabilities as values.

Return type:

dict

plot(qubit_subset: Sequence[int] | None = None) None[source]

TODO: calling this will error because of matplotlib

Plots a bar chart with bitstring on the x axis and probability on the y axis.

Parameters:

qubit_subset – Optional parameter used for plotting a subset of the Hilbert space.

pretty_print(decimal_digits: int = 2) str[source]

Returns a string repr of the wavefunction, ignoring all outcomes with approximately zero amplitude (up to a certain number of decimal digits) and rounding the amplitudes to decimal_digits.

Parameters:

decimal_digits (int) – The number of digits to truncate to.

Returns:

A string representation of the wavefunction.

pretty_print_probabilities(decimal_digits: int = 2) Dict[str, float][source]

TODO: This doesn’t seem like it is named correctly…

Prints outcome probabilities, ignoring all outcomes with approximately zero probabilities (up to a certain number of decimal digits) and rounding the probabilities to decimal_digits.

Parameters:

decimal_digits (int) – The number of digits to truncate to.

Returns:

A dict with outcomes as keys and probabilities as values.

probabilities() ndarray[source]

Returns an array of probabilities in lexicographical order

sample_bitstrings(n_samples: int) ndarray[source]

Sample bitstrings from the distribution defined by the wavefunction.

Parameters:

n_samples – The number of bitstrings to sample

Returns:

An array of shape (n_samples, n_qubits)

static zeros(qubit_num: int) Wavefunction[source]

Constructs the groundstate wavefunction for a given number of qubits.

Parameters:

qubit_num

Returns:

A Wavefunction in the ground state

pyquil.wavefunction.get_bitstring_from_index(index: int, qubit_num: int) str[source]

Returns the bitstring in lexical order that corresponds to the given index in 0 to 2^(qubit_num) :param int index: :param int qubit_num: :return: the bitstring :rtype: str