hqs_qorrelator_app

HQS Qorrelator App Python interface.

Copyright © 2021-2024 HQS Quantum Simulations GmbH. All Rights Reserved.

NMRCorrelator()

Create a new NMRCorrelator to calculate the correlation functions for NMR experiments.

Functions

fft_causal(f)

Performs the Fourier Transform of a causal signal separating its odd and even part.

Classes

InfiniteTemperatureCorrelator()

Class constructing qoqo measurements for infinite temperature correlators.

NMRCorrelator()

Create a new NMRCorrelator to calculate the correlation functions for NMR experiments.

class hqs_qorrelator_app.InfiniteTemperatureCorrelator

Class constructing qoqo measurements for infinite temperature correlators.

The user can access the following options in order to tailor the InfiniteTemperatureCorrelator class: * algorithm: the algorithm used for simulating the time evolution of a spin Hamiltonian on the quantum computer * initialisation: the type of initialisation to prepare the infinite temperature state * number_measurements: the number of measurements used in the circuit

These can be changed using setters, as shown in the example below.

Example

>>> from py_alqorithms import InfiniteTemperatureCorrelator
... correlator = InfiniteTemperatureCorrelator()
... correlator.algorithm = "ParityBased"
Returns:

A new InfiniteTemperatureCorrelator object.

algorithm

Setter for the algorithm option for InfiniteTemperatureCorrelator.

Four options are available:
ParityBased: An algorithm that implements the time evolution under a product of PauliOperators

via the total Parity of the set of involved qubits. The total parity is perpared in the last involved qubit.

QSWAP: Terms in the Hamiltonian involving two qubits are implemented using a swap network

that reorders the terms. Uses the ParityBased algorithm for multi qubit terms.

QSWAPMolmerSorensen: The QSWAP algorithm using variable angle Mølmer Sørensen XX interaction

gates

VariableMolmerSorensen: An algorithm that implements terms in the Hamiltonian

involving two qubits with a basis change and variable angle Mølmer Sørensen gates

Parameters:

algorithm (str) -- The option for the algorithm that is set.

static from_bincode(input)

Convert the bincode representation of the SpinCorrelatorApp to a SpinCorrelatorApp using the [bincode] crate.

Parameters:

input (ByteArray) -- The serialized InfiniteTemperatureCorrelator (in [bincode] form).

Returns:

The deserialized InfiniteTemperatureCorrelator.

Return type:

InfiniteTemperatureCorrelator

Raises:
  • TypeError -- Input cannot be converted to byte array.

  • ValueError -- Input cannot be deserialized to InfiniteTemperatureCorrelator.

static from_json(input)

Convert the json representation of a InfiniteTemperatureCorrelator to a InfiniteTemperatureCorrelator.

Parameters:

input (str) -- The serialized InfiniteTemperatureCorrelator in json form.

Returns:

The deserialized InfiniteTemperatureCorrelator.

Return type:

InfiniteTemperatureCorrelator

Raises:

ValueError -- Input cannot be deserialized to InfiniteTemperatureCorrelator.

initialisation

Setter for the initialisation option of InfiniteTemperatureCorrelator.

Parameters:

initialisation (str) -- The option for the initialisation that is set. At the moment the available options are: all_initial_states, positive_magnetization and random_states.

Raises:

ValueError -- Option not available.

number_measurements

Setter for the number of projective measurements used when measuring observables.

Parameters:

number_measurements (int) -- The new number of measurements.

number_samples

Setter for the number of samples to reconstruct the trace when the initialisation RandomStates is selected.

Parameters:

number_samples (int) -- The new number of samples.

time_correlation_measurement(left_operator, hamiltonian, right_operator, trotter_timestep)

Create a [roqoqo::measurements::PauliZProduct] measurement to measure a correlator.

The resulting measurement will calculate Tr[A(t) B rho]

where rho is the infinite temperature steady state, A and B are PauliOperators and the time evolution of A is given by a hamiltonian H. The time t of the correlator is a symbolic variable in the measurement with name time

Parameters:
  • left_operator (spins.PauliOperator) -- Left operator A

  • hamiltonian (spins.PauliHamiltonian) -- Hamiltonina H determining the time evolution

  • right_operator (spins.PauliOperator) -- Right operator B

Returns:

The measurement that can calculate the correlation function

Return type:

PauliZProduct

time_correlation_measurement_multiple_left_fixed_timestep(left_input_operators, left_operator_names, hamiltonian, right_operator, trotter_timestep)

Create a [roqoqo::measurements::PauliZProduct] measurement to measure several correlators.

The resulting measurement will calculate a dictionay of Tr[A_i(n*trotter_timestep) B rho]

where A_i are PauliOperators and the keys in the dict are given by a list of provided names, rho is the infinite temperature steady state, B is a PauliOperator and the time evolution of the A_i is given by a hamiltonian H. The time n*trotter_timestep of the correlator is a multiple of a given Trotter step. The variable n is a symbolic value with the name number_trottersteps.

Parameters:
  • left_input_operators (List[PauliOperator]) -- List of left operators A_i

  • left_operator_names (List[str]) -- List of names of operator A_i

  • hamiltonian (PauliHamiltonian) -- Hamiltonina H determining the time evolution

  • right_operator (PauliOperator) -- Right operator B

  • trotter_timestep (float) -- The fixed trotter timestep

Returns:

The measurement that can calculate the correlation function

Return type:

PauliZProduct

to_bincode()

Return the bincode representation of the InfiniteTemperatureCorrelator using the [bincode] crate.

Returns:

The serialized InfiniteTemperatureCorrelator (in [bincode] form).

Return type:

ByteArray

Raises:

ValueError -- Cannot serialize InfiniteTemperatureCorrelator to bytes.

to_json()

Return the json representation of the InfiniteTemperatureCorrelator.

Returns:

The serialized form of InfiniteTemperatureCorrelator.

Return type:

str

Raises:

ValueError -- Cannot serialize InfiniteTemperatureCorrelator to json.

class hqs_qorrelator_app.NMRCorrelator

Create a new NMRCorrelator to calculate the correlation functions for NMR experiments.

The user can access the following options in order to tailor the NMRCorrelator class: * parallelization_blocks: whether to use parallelization_blocks noise insertion mode,

or the all_qubits noise insertion mode (default)

  • algorithm: the algorithm used for simulating the time evolution of a spin Hamiltonian on the quantum computer

  • initialisation: the type of initialisation to prepare the infinite temperature state

  • number_measurements: the number of measurements used in the circuit

  • b_field_direction: the direction of the external magnetic field for the NMR simulation

  • decomposition_blocks: whether to use decomposition blocks in the circuits

These can be changed using setters, as shown in the example below.

Example

>>> from hqs_qorrelator_app import NMRCorrelator
... correlator = NMRCorrelator()
... correlator.algorithm = "ParityBased"
Returns:

A new NMRCorrelator object.

algorithm

Setter for the algorithm option for NMRCorrelator.

Four options are available:
ParityBased: An algorithm that implements the time evolution under a product of PauliOperators

via the total Parity of the set of involved qubits. The total parity is perpared in the last involved qubit.

QSWAP: Terms in the Hamiltonian involving two qubits are implemented using a swap network

that reorders the terms. Uses the ParityBased algorithm for multi qubit terms.

QSWAPMolmerSorensen: The QSWAP algorithm using variable angle Mølmer Sørensen XX interaction

gates

VariableMolmerSorensen: An algorithm that implements terms in the Hamiltonian

involving two qubits with a basis change and variable angle Mølmer Sørensen gates

Parameters:

algorithm (str) -- The option for the algorithm that is set.

b_field_direction

Setter for the b_field_direction option of NMRCorrelator.

This is the direction of the external magnetic field for the NMR simulation. If the field direction in the input Hamiltonian does not match this option, the NMRCorrelator will transform the Hamiltonian accordingly.

Parameters:

initialisation (str) -- The option for the b_field_direction that is set. Available options are X and Z.

Raises:

ValueError -- Option not available.

decomposition_blocks

Setter for the decomposition_blocks option of the NMRCorrelator.

Parameters:

decomposition_blocks (bool) -- Whether to activate the decomposition blocks option.

estimate_decoherence_rate(trotter_timestep, device, noise_models)

Calculates an estimate of the overall decoherence rate of the problem.

This will not perform full noise mapping but estimate the overall decoherence rate of the problem by summing up the decoherence rates of the individual noise contributions between the unitary gates, using the fact that the trace over the decoherence matris is invariant under unitary transformations.

Example

>>> from hqs_qorrelator_app import NMRCorrelator
... from struqture_py import spins
... import numpy as np
... from qoqo.devices import AllToAllDevice
...
... number_qubits = 2
... coupling = 1.0
... shift = 1.0
...
... hamiltonian = spins.PauliHamiltonian()
... for i in range(number_qubits):
...     hamiltonian.add_operator_product(spins.PauliProduct().z(i), -shift * i)
... for i in range(number_qubits - 1):
...     hamiltonian.add_operator_product(spins.PauliProduct().x(i).x(i + 1), coupling)
...     hamiltonian.add_operator_product(spins.PauliProduct().y(i).y(i + 1), coupling)
... device = AllToAllDevice(number_qubits, ["RotateX", "RotateZ"], ["CNOT"], 1.0).add_dephasing_all(0.001);
... correlator = NMRCorrelator()
...
... rate = correlator.estimate_decoherence_rate(hamiltonian, 0.1, device)
Parameters:
  • hamiltoninan (PauliHamiltonian) -- The quantum circuit simulating one Trotterstep.

  • trotter_timestep (float) -- The Trotter timestep that is simulated by the circuit.

  • device (qoqo.Device) -- The device representing the connectivity.

  • noise_models (List[NoiseModel]) -- Noise models determining noise properties.

Returns:

The estimated noise rate.

Return type:

CaclulatorFloat

Raises:
  • TypeError -- circuit argument cannot be converted to qoqo Circuit.

  • RuntimeError -- Extracting effective noise model failed.

from_bincode()

Convert the bincode representation of the NMRCorrelator to a NMRCorrelator using the [bincode] crate.

Parameters:

input (ByteArray) -- The serialized NMRCorrelator (in [bincode] form).

Returns:

The deserialized NMRCorrelator.

Return type:

NMRCorrelator

Raises:
  • TypeError -- Input cannot be converted to byte array.

  • ValueError -- Input cannot be deserialized to NMRCorrelator.

from_json()

Convert the json representation of a NMRCorrelator to a NMRCorrelator.

Parameters:

input (str) -- The serialized NMRCorrelator in json form.

Returns:

The deserialized NMRCorrelator.

Return type:

NMRCorrelator

Raises:

ValueError -- Input cannot be deserialized to NMRCorrelator.

initialisation

Setter for the initialisation option of NMRCorrelator.

Please note that, while the following options exist, only the all_initial_states option is currently supported: active_reset, measurement, dephasing, local_operator_measurement, all_initial_states.

Parameters:

initialisation (str) -- The option for the initialisation that is set.

Raises:

ValueError -- Option not available.

insert_noise(device, noise_models)

Insert noise Pragmas into Quantum porgram according to chosen noise model

Parameters:
  • program (qoqo.QuantumProgram)

  • device (qoqo.Device)

  • noise_models (List[NoiseModel]) -- Noise models determining noise properties.

Returns:

The QuantumProgram with noise inserted.

Return type:

QuantumProgram

Raises:
  • TypeError -- Input could not be converted to QuantumProgram.

  • TypeError -- Input could not be converted to qoqo Device.

  • TypeError -- Input could not be converted to List[NoiseModel].

  • RuntimeError -- Could not insert noise into the QuantumProgram.

noisy_algorithm_model(trotter_timestep, device, noise_models)

Returns the effective noise model derived from the circuit of one Trotterstep.

In this function the circuit simulating the time evolution for one Trotterstep is constructed from the provided Hamiltonian, the provied length of a Trotterstep and all internal parameters of the NMRCorrelator.

Example

>>> from hqs_qorrelator_app import NMRCorrelator
... from struqture_py import spins
... import numpy as np
... from qoqo.devices import AllToAllDevice
...
... number_qubits = 2
... coupling = 1.0
... shift = 1.0
...
... hamiltonian = spins.PauliHamiltonian()
... for i in range(number_qubits):
...     hamiltonian.add_operator_product(spins.PauliProduct().z(i), -shift * i)
... for i in range(number_qubits - 1):
...     hamiltonian.add_operator_product(spins.PauliProduct().x(i).x(i + 1), coupling)
...     hamiltonian.add_operator_product(spins.PauliProduct().y(i).y(i + 1), coupling)
... device = AllToAllDevice(number_qubits, ["RotateX", "RotateZ"], ["CNOT"], 1.0).add_dephasing_all(0.001);
... correlator = NMRCorrelator()
...
... noise_model = correlator.noisy_algorithm_model(hamiltonian, 0.1, device)

Note that the superoperator corresponds to the continuous time evolution of a density matrix but has been derived from a Trotterstep with a discrete time-step. This approximation is only valid if the time-step is short enough compared to the energy scales in the system that the Trotterization errors of the unitary evolution and the noise terms are small.

Parameters:
  • hamiltoninan (PauliHamiltonian) -- The quantum circuit simulating one Trotterstep.

  • trotter_timestep (float) -- The Trotter timestep that is simulated by the circuit.

  • device (qoqo.Device) -- The device representing the connectivity.

  • noise_models (List[NoiseModel]) -- Noise models determining noise properties.

Returns:

the effective noise system operator derived from the circuit of one Trotterstep.

Return type:

PauliLindbladNoiseOperator

Raises:
  • TypeError -- circuit argument cannot be converted to qoqo Circuit.

  • RuntimeError -- Extracting effective noise model failed.

number_measurements

Setter for the number of projective measurements used when measuring observables.

Parameters:

number_measurements (int) -- The new number of measurements

number_samples

Setter for the number of samples of initial states for the RandomStates initialization option.

Parameters:

number_samples (int) -- The new number of samples

parallelization_blocks

Setter for the parallelization blocks for noise insertion.

This controls the noise_mode of the NMRCorrelator:
If the parallelization input is false: the noise mode remains as AllQubits, which

is the default. In this case, the noise model adds noise on all qubits that are involved in the whole quantum circuit after each gate operation.

If the parallelization input is true, the noise mode is changed to

ParallelizationBlocks. In this case, the noise model adds noise on the qubits that are involved in a block or parallel operations. This noise is added after a PragmaStopParallelBlock that notifies the end of the parallel operations in a qoqo circuit.

Parameters:

parallelization (bool) -- The boolean indicating whether to use parallelization blocks for noise insertion, instead of noise on all qubits.

spectrum_program_fixed_step(trotter_timestep, gyromagnetic_factors, device)

Creates a QuantumProgram to measure time dependent NMR Magnetization correlators.

This method uses a fixed Trotter time-step and uses number-trottersteps as a free parameter.

The Hamiltonian of NRM systems contain different frequency scales. A large frequency Zeeman splitting and on a much lower frequency scale the chemical shifts of the on-site PauliZ terms and the interaction between spins.

This function assumes that the Spin-Hamiltonian has already been processed reduce the system to spins very close in Zeeman frequency and that the Rotating Wave Approximation (RWA) has been applied.

The Hamiltonian will have the form

H = sum gj * B * dj * B * Zj + sum Jjk (Xk Xk + YjYk + ZjZk)

where Zj, Yj and Xj are the Pauli Z, Y and X operators on site j and the sum goes over all indices. gj is the gyromagnetic factor dj are the chemical shifts and Jjk is the coupling between spins.

The quantity one is interested in in NMR is the correlator

C(t) = Tr(Splus(t) Sminus(0) rho)

where Splus is the plus magnetization operator Splus = sum gj (Xj - i Yj) / 4 and Sminus is the minus magnetization operator Sminus = sum gj (Xj + i Yj) / 4 and rho is the density matrix. Here we assume the system to be at infinite temperature compared to all other energy scales in the problem and rho is the identity matrix.

The quantum computer cannot simulate the evolution of Hamiltonians with a large separation of frequency scales accurately. The large energy scale (gj * B) is already eliminated from the Hamiltonian in the rotating wave approximation. The function will construct a quantum program that calculates the real and imaginary parts of C(t) which only involves simmulating the long time / low frequency part of the problem on the quantum computer.

Parameters:
  • hamiltonian (PauliHamiltonian) -- The Hamiltonian of the Spin system, already in RWA form.

  • trotter_timestep (float) -- One Trotterstep propagates the system under the hamiltonian for this time. Smallest time increment when running simulations.

  • gyromagnetic_factors (List[float]) -- The gyromagnetic factors for scaling the magnetization operators.

  • device (qoqo.Device) -- The device for which to create the QuantumProgram.

Returns:

A qoqo QuantumProgram for calculating the correlation function for a time.

The program takes time as the free parameter.

Return type:

QuantumProgram

Raises:
  • TypeError -- Input could not be converted to PauliHamiltonian.

  • TypeError -- Input could not be converted to qoqo Device.

  • RuntimeError -- Creating the QuantumProgram failed.

Example

>>> from hqs_qorrelator_app import NMRCorrelator
... from struqture_py import spins
... import numpy as np
... from qoqo_quest import backend
... from qoqo.devices import AllToAllDevice
...
...
... gyromagnetic = 1.0
... number_qubits = 2
... time = np.pi/4
... coupling = 1.0
... shift = 1.0
... gyromagnetic_factors = [gyromagnetic for _ in range(number_qubits)]
...
... hamiltonian = spins.PauliHamiltonian()
... for i in range(number_qubits):
...     hamiltonian.add_operator_product(spins.PauliProduct().z(i), -shift * i)
... for i in range(number_qubits - 1):
...     hamiltonian.add_operator_product(spins.PauliProduct().x(i).x(i + 1), coupling)
...     hamiltonian.add_operator_product(spins.PauliProduct().y(i).y(i + 1), coupling)
...
... nmr = NMRCorrelator()
... device = AllToAllDevice(number_qubits, ["RotateX", "RotateZ"], ["CNOT"], 1.0).add_dephasing_all(0.001);
... trotter_timestep = 0.01
... program = nmr.spectrum_program_fixed_step(hamiltonian, trotter_timestep, gyromagnetic_factors, device)
... backend = Backend(number_qubits)
... result = program.run([time], backend)
...
...
thermalization_fidelity(trotter_timestep, device, noise_models)

Returns the thermalization_fidelity when applying one trotter timestep under the Hamiltonian

During the calculation of the time-resolved correlation functions, the quantum computer propagates the state with a trotterization algorithm. The noise of the physical quantum computer leads to an effective hamiltonian noise model. In the ideal case, this effective noise will correspond to infinite temperature thermalization. In general, however, the effective noise model will not exactly correspond to infinite temperature. The noise model can contain biased noise rates that drive the system towards a state that is not the fully mixed state (which corresponding to infinite temperature). The thermalization fidelity is an approximate measure for the quality of the effective noise. It is given by 1 - sum of biased rates / sum of unbiased rates. A thermalization fidelity of 1 corresponds to a noise model closely matching infinite temperature environments and a thermalization fidelity of 0 corresponds to an environment that is far away from infinite temperature.

When calculating the thermalization fidelity, not all rates are categorized into biased or unbiased rates. These rates are discarded. The discarded weight gives the ratio between the discarded rates and all rates. A value of 0 corresponds to a reliable value for the thermalization fidelity.

Parameters:
  • hamiltonian (PauliHamiltonian) -- The Hamiltonian for which thermalization fidelity is calculated.

  • trotter_timestep (float) -- The Trotter timestep

  • device (qoqo.Device) -- The device determining the connectivity.

  • noise_models (List[NoiseModel]) -- Noise models determining noise properties.

Returns:

(infinite temperature thermalization fidelity, discarded weight)

Raises:
  • TypeError -- Input could not be converted to PauliHamiltonian.

  • TypeError -- Input could not be converted to qoqo Device.

  • TypeError -- Input could not be converted to List[NoiseModel].

  • RuntimeError -- Extracting effective noise model failed.

to_bincode()

Return the bincode representation of the NMRCorrelator using the [bincode] crate.

Returns:

The serialized NMRCorrelator (in [bincode] form).

Return type:

ByteArray

Raises:

ValueError -- Cannot serialize NMRCorrelator to bytes.

to_json()

Return the json representation of the NMRCorrelator.

Returns:

The serialized form of NMRCorrelator.

Return type:

str

Raises:

ValueError -- Cannot serialize NMRCorrelator to json.

hqs_qorrelator_app.fft_causal(f: ndarray) ndarray[source]

Performs the Fourier Transform of a causal signal separating its odd and even part.

This way of transforming is necessary for causal signals or any signal that does not start and end at the same value. This is because the fft replicates the signal to be able to calculate the coefficients, leading to sudden jumps if the function itself is not periodic. These jumps introduce a shift in the FT at all frequencies, being equivalent to a delta within the sampled time frame. By (anti-)symmetrizing the signal one can circumvent this problem and obtain the correct Fourier Transform.

Parameters:

f (np.ndarray) -- Function to transform.

Returns:

The complex Fourier Transform.

Return type:

np.ndarray