qml.data.Dataset

class Dataset(*args, standard=False, **kwargs)[source]

Bases: abc.ABC

Create a dataset object to store a collection of information describing a physical system and its evolution. For example, a dataset for an arbitrary quantum system could have a Hamiltonian, its ground state, and an efficient state-preparation circuit for that state.

Parameters
  • *args – For internal use only. These will be ignored if called with standard=False

  • standard (bool) – For internal use only. See the note below for the behavior when this is set to True

  • **kwargs – variable-length keyword arguments specifying the data to be stored in the dataset

Note on the standard kwarg:

A standard Dataset uses previously generated, hosted quantum data. This special instance of the Dataset class makes certain assumptions about folder management for downloading the data and handling I/O. As such, the Dataset class should not be instantiated by the users directly with standard=True. Instead, they should use load().

See also

load()

Example

>>> Hamiltonian = qml.Hamiltonian([1., 1.], [qml.PauliZ(wires=0), qml.PauliZ(wires=1)])
>>> eigvals, eigvecs = np.linalg.eigh(qml.matrix(Hamiltonian))
>>> ground_state_energy = np.min(eigvals)
>>> ground_state = np.transpose(eigvecs)[np.argmin(eigvals)]
>>> dataset = qml.data.Dataset(Hamiltonian = Hamiltonian, ground_state = ground_state,
        ground_state_energy = ground_state_energy)
>>> print(dataset.Hamiltonian)
      (1) [Z0]
    + (1) [Z1]
>>> print(dataset.ground_energy)
-2.0

In addition to creating datasets in memory, we can also store them in the disk and then load them as follows. First we create the dataset:

>>> Hamiltonian = qml.Hamiltonian([1., 1.], [qml.PauliZ(wires=0), qml.PauliZ(wires=1)])
...     eigvals, eigvecs = np.linalg.eigh(qml.matrix(Hamiltonian))
>>> ground_state_energy = np.min(eigvals)
>>> ground_state = np.transpose(eigvecs)[np.argmin(eigvals)]
>>> dataset = qml.data.Dataset(Hamiltonian = Hamiltonian, ground_state = ground_state,
...     ground_state_energy = ground_state_energy)

Then to save the dataset to a file, we call Dataset.write():

>>> dataset.write('./path/to/file/dataset.dat')

We can then retrieve the data using Dataset.read()

>>> retrieved_data = qml.data.Dataset()
>>> retrieved_data.read('./path/to/file/dataset.dat')
>>> print(retrieved_data.Hamiltonian)
  (1) [Z0]
+ (1) [Z1]
>>> print(dataset.Hamiltonian)
  (1) [Z0]
+ (1) [Z1]

attrs

Returns attributes of the dataset.

attrs

Returns attributes of the dataset.

list_attributes()

List the attributes saved on the Dataset

read(filepath[, lazy, assign_to])

Loads data from a saved file to the current dataset.

write(filepath[, protocol])

Writes the dataset to a file as a dictionary.

list_attributes()[source]

List the attributes saved on the Dataset

read(filepath, lazy=False, assign_to=None)[source]

Loads data from a saved file to the current dataset.

Parameters
  • filepath (string) – The desired location and filename to load, e.g. ‘./path/to/file/file_name.dat’.

  • lazy (bool) – Indicates if only the key of the attribute should be saved to the Dataset instance. Note that the file will be remembered and its contents will be loaded when the attribute is used.

  • assign_to (str) – Attribute name to which the contents of the file should be assigned. If this is None (the default value), this method will assume that the file contents are of the form {attribute_name: attribute_value,}.

Example

>>> new_dataset = qml.data.Dataset(kw1 = 1, kw2 = '2', kw3 = [3])
>>> new_dataset.read('./path/to/file/file_name.dat')

Using the assign_to keyword argument:

>>> new_dataset = qml.data.Dataset()
>>> new_dataset.read('./path/to/file/single_state.dat', assign_to="state")
>>> new_dataset.state  # assuming the above file contains only a tensor
tensor([1, 1, 0, 0], requires_grad=True)
write(filepath, protocol=4)[source]

Writes the dataset to a file as a dictionary.

Parameters

filepath (string) – the desired save location and file name

Example

>>> new_dataset = qml.data.Dataset(kw1 = 1, kw2 = '2', kw3 = [3])
>>> new_dataset.write('./path/to/file/file_name.dat')