dynamite.states

class dynamite.states.State(state=None, subspace=None, L=None, seed=None)[source]

Bases: object

Class representing a state.

Parameters:
  • state (int or str, optional) – An initial product state to set the state to. Also accepts 'random' and 'uniform'. The state can also be initialized later with the set_product(), set_uniform() and set_random() methods.

  • subspace (dynamite.subspace.Subspace, optional) – The subspace on which the state should be defined. If not specified, defaults to config.subspace.

  • L (int, optional) – Spin chain length. Can be ommitted if config.L is set, a subspace is provided, or a string is passed to the state argument.

  • seed (int, optional) – If the state argument is set to 'random', the seed for the random number generator. This argument is ignored otherwise.

property L
copy(result=None)[source]
property subspace

The space on which the vector is defined.

See dynamite.subspaces for details.

property vec

The PETSc vector containing the state data.

petsc4py Vec methods can be used through this interface–for example, to find the norm of a State s, one can do state.vec.norm(). The methods don’t seem to be documented anywhere, but are fairly transparent from looking at the petsc4py source code.

property initialized

Whether the state vector data has been set yet.

set_initialized()[source]

Mark that the state vector data has been set. This method does not normally need to be called by the user, unless the vector data is set manually by directly accessing the underlying PETSc vector.

assert_initialized()[source]

Raise an exception if the vector has not been initialized

classmethod str_to_state(s, L)[source]

Convert a string to an integer whose bitwise representation is the spin configuration (0=↑, 1=↓) of a product state. The characters ‘D’ and ‘U’ represent down and up spins, like "DUDDU...UDU" (D=↓, U=↑).

Note

For the string representation, the leftmost character is spin index 0. For an integer representation, the rightmost (least significant) bit is!

Parameters:
  • s (int or string) – The state. If an integer is passed, the same integer is returned.

  • L (int) – The length of the spin chain

Returns:

The state

Return type:

int

set_product(s)[source]

Initialize to a product state. Can be specified either be an integer whose binary representation represents the spin configuration (0=↑, 1=↓) of a product state, or a string of the form "DUDDU...UDU" (D=↓, U=↑) or "10110...010" (1=↓, 0=↑). If it is a string, the string’s length must equal L.

Parameters:

s (int or str) – A representation of the state.

set_uniform()[source]

Set the state to a uniform superposition over all basis states. If the State object has its subspace set to something other than Full (the default), the superposition will be over that subspace’s basis states.

classmethod generate_time_seed()[source]
set_random(seed=None, normalize=True)[source]

Initialized to a normalized random state.

Note

When running under MPI with multiple processes, the seed is incremented by the MPI rank, so that each process generates different random values.

Parameters:
  • seed (int, optional) – A seed for numpy’s PRNG that is used to build the random state. The user should pass the same value on every process.

  • normalize (bool) – Whether to rescale the random state to have norm 1.

set_all_by_function(val_fn, vectorize=False)[source]

Evaluate the given function along the entire vector, and set each vector element to the result. val_fn should take a single argument, which is an integer representation of a state (ones place represents spin 0), and return the corresponding vector element value. Ideally val_fn should be written so that it can be applied to a vector of integers instead of a single integer, and returns a corresponding vector of values, in which case vectorize can be set to True to significantly improve performance.

Parameters:
  • val_fn (function) – A function taking an integer (representing a product state) and outputting a complex number

  • vectorize (bool, optional) – Whether to apply val_fn in a vectorized manner

entanglement_entropy(keep)

Compute the entanglement of a state across some cut on the spin chain. To be precise, this is the bipartite entropy of entanglement.

Currently, this quantity is computed entirely on process 0. As a result, the function returns -1 on all other processes.

Parameters:
Returns:

The entanglement entropy

Return type:

float

project(index, value)[source]

Project the state onto a subspace in which the qubit at the given index has been projected onto the given value (0 or 1). In other words, perform a projective measurement, post-selecting on the outcome.

Projection done in-place, function returns None.

Parameters:
  • index (int) – The spin index to project

  • value (0 or 1) – Which single-spin state to project onto

to_numpy(to_all=False)[source]

Return a numpy representation of the state.

Parameters:

to_all (bool) – Whether to return the vector on all MPI ranks (True), or just rank 0 (False).

save(fname)[source]

Save the state to disk. Note that this method saves the state as a pair of files, <fname>.vec and <fname>.metadata.

Parameters:

fname (str) – The path to save the state to

classmethod from_file(fname)[source]

Load a state from a file. You do not need to create a state to call this method—just directly call State.from_file(fname) and the return value will be your new state object.

Note

This method uses the Python pickle module which is not secure against maliciously constructed data; thus this method should not be used on data from untrusted sources.

Parameters:

fname (str) – The path from which to load the state

Returns:

The state from the file

Return type:

State

dot(x)[source]

Compute the inner product of two states.

Parameters:

x (State) – The state to take the inner product against

Returns:

The value of the inner product

Return type:

complex

norm()[source]

Compute the Euclidean norm of the state vector.

Returns:

The vector norm

Return type:

float

normalize()[source]

Scale the vector such that the norm is 1.

scale(c)[source]

Scale the vector.

Parameters:

c (float) – The value to scale by

axpy(alpha, x)[source]

When y.axpy(alpha, x) is called, it scales the vector x by the scalar alpha, and sums the result into y. In other words, it computes y = alpha*x + y.

scale_and_sum(alpha, beta, x)[source]

Also known as “axpby”, when y.scale_and_sum(alpha, beta, x) is called, it computes y = alpha*x + beta*y.

exception dynamite.states.UninitializedError[source]

Bases: RuntimeError