utils

This module contains several auxiliary functions needed by other scripts of the library

Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro Costas

Fast Walsh-Hadamard Transform is based on mex function written by Chengbo Li@Rice Uni for his TVAL3 algorithm: https://github.com/dingluo/fwht/blob/master/FWHT.py

QQuantLib.utils.utils.bitfield(n_int: int, size: int)

Transforms an int n_int to the corresponding bitfield of size size

Parameters:
  • n_int (int) – integer from which we want to obtain the bitfield

  • size (int) – size of the bitfield

Returns:

full – bitfield representation of n_int with size size

Return type:

list of ints

QQuantLib.utils.utils.bitfield_to_int(lista)

Transforms the bitfield list to the corresponding int :param lista: bitfield :type lista: ist of ints

Returns:

integer – integer obtained from it’s binary representation.

Return type:

int

QQuantLib.utils.utils.check_list_type(x_input, tipo)

Check if a list x_input is of type tipo :param x_input: :type x_input: list :param tipo: it has to be understandable by numpy :type tipo: data type

Returns:

y_output – numpy array of type tipo.

Return type:

np.array

QQuantLib.utils.utils.expmod(n_input: int, base: int)

For a pair of integer numbers, performs the decomposition:

\[n_input = base^power+remainder\]
Parameters:
  • n_input (int) – number to decompose

  • base (int) – basis

Returns:

  • power (int) – power

  • remainder (int) – remainder

QQuantLib.utils.utils.fwht(x_input: numpy.array, ordering: str = 'sequency')

Fast Walsh Hadamard transform of array x_input Works as a wrapper for the different orderings of the Walsh-Hadamard transforms.

Parameters:
  • x_input (numpy array)

  • ordering (string) – desired ordering of the transform

Returns:

y_output – Fast Walsh Hadamard transform of array x_input in the corresponding ordering

Return type:

numpy array

QQuantLib.utils.utils.fwht_dyadic(x_input: numpy.array)

Fast Walsh-Hadamard Transform of array x_input in dyadic ordering The result is not normalised Based on mex function written by Chengbo Li@Rice Uni for his TVAL3 algorithm. His code is according to the K.G. Beauchamp’s book – Applications of Walsh and Related Functions. :param array: :type array: numpy array

Returns:

x_output – Fast Walsh Hadamard transform of array x_input.

Return type:

numpy array

QQuantLib.utils.utils.fwht_natural(array: numpy.array)

Fast Walsh-Hadamard Transform of array x in natural ordering The result is not normalised :param array: :type array: numpy array

Returns:

fast_wh_transform – Fast Walsh Hadamard transform of array x.

Return type:

numpy array

QQuantLib.utils.utils.fwht_sequency(x_input: numpy.array)

Fast Walsh-Hadamard Transform of array x_input in sequence ordering The result is not normalised Based on mex function written by Chengbo Li@Rice Uni for his TVAL3 algorithm. His code is according to the K.G. Beauchamp’s book – Applications of Walsh and Related Functions. :param x_input: :type x_input: numpy array

Returns:

x_output – Fast Walsh Hadamard transform of array x_input.

Return type:

numpy array

QQuantLib.utils.utils.get_histogram(probability, low_limit, high_limit, nbin)

Given a function probability, convert it into a histogram. The function must be positive, the normalization is automatic. Note that instead of having an analytical expression, probability could just create an arbitrary vector of the right dimensions and positive amplitudes. This procedure could be used to initialize any quantum state with real amplitudes

Parameters:
  • low_limit (float) – lower limit of the interval

  • high_limit (float) – upper limit of the interval

  • probability (function) – function that we want to convert to a probability mass function It does not have to be normalized but must be positive in the interval

  • nbin (int) – number of bins in the interval

Returns:

  • centers (np.darray) – numpy array with the centers of the bins of the histogram

  • probs (np.darray) – numpy array with the probability at the centers of the bins of the histogram

QQuantLib.utils.utils.left_conditional_probability(initial_bins, probability)

This function calculate f(i) according to the Lov Grover and Terry Rudolph 2008 papper: ‘Creating superposition that correspond to efficiently integrable probability distributions’ http://arXiv.org/abs/quant-ph/0208112v1

Given a discretized probability and an initial number of bins the function splits each initial region in 2 equally regions and calculates the conditional probabilities for x is located in the left part of the new regions when x is located in the region that contains the corresponding left region

Parameters:
  • initial_bins (int) – Number of initial bins for splitting the input probabilities

  • probability (np.darray.) – Numpy array with the probabilities to be load. initial_bins <= len(Probability)

Returns:

left_cond_prob – conditional probabilities of the new initial_bins+1 splits

Return type:

np.darray

QQuantLib.utils.utils.load_qn_gate(qlm_gate, n_times)

Create an AbstractGate by applying an input gate n times

Parameters:
  • qlm_gate (QLM gate) – QLM gate that will be applied n times

  • n_times (int) – number of times the qlm_gate will be applied

QQuantLib.utils.utils.mask(number_qubits, index)

Transforms the state \(|index\rangle\) into the state \(|11...1\rangle\) of size number qubits.

Parameters:
  • number_qubits (int)

  • index (int)

Returns:

mask – the gate that we have to apply in order to transform state \(|index\rangle\). Note that it affects all states.

Return type:

Qlm abstract gate

QQuantLib.utils.utils.measure_state_probability(input_result, target)

From an input result DataFrame gets the probability of target state

Parameters:
  • input_result (Pandas DataFrame) – DataFrame with measurement results like obtained in the get_results function (from QQuantLib.utils.data_extracting)

  • target (list) – python list with the state we want to extract

Returns:

output_probability – Probability of the desired target state. If the state it is not found then 0.0 is returned.

Return type:

float

QQuantLib.utils.utils.oracle_shots_calculation(m_k, n_k)

Function for computing the total number of oracle shots.

Parameters:
  • m_k (list) – list with integers. Applications of the Grover-like amplification operator.

  • n_k (list) – list with integers. Number of shots for each value of m_k.

Returns:

oracle_shots – Number of total oracle calls for the input schedule

Return type:

int

QQuantLib.utils.utils.test_bins(array, text='probability')

Testing condition for numpy arrays. The length of the array must be 2^n with n an int. :param array: Numpy Array whose dimensionality is going to test :type array: np.ndarray :param test: String for identification purposes :type test: str

Raises:

AssertionError – If lengt of array is not 2^n with n an int.

Returns:

nqbits – Minimum number of qbits mandatory for storing input array in a quantum state

Return type:

int

QQuantLib.utils.utils.text_is_none(variable, variable_name, variable_type=<class 'float'>)

Raise an exception if variable is None