QQuantLib.utils

QQuantLib.utils.benchmark_utils

Utils functions from benchmark purpouses.

Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro

tnbs.BTC_02_AE.QQuantLib.utils.benchmark_utils.combination_for_dictionary(input_dict)

Creates a list of dictionaries with all the posible combination of the input dictionary.

Parameters:

input_dict (python dictionary) – python dictionary where each key value MUST be a list. For each value of a list a new dictioanry will be created

Returns:

list_of_dictionaries – A list with all posible combination of dictionaries from the input dictionary

Return type:

list of python dictionaries

tnbs.BTC_02_AE.QQuantLib.utils.benchmark_utils.combination_for_list(input_list)

For each dictionary of the list the function creates all posible combinations. All the posible combinations are concatenated.

Parameters:

input_list (list of python dictionary) – The values of each key of the each python dictionary MUST BE lists.

Returns:

list_of_combinations – A list with the concatenation of all posible combinations for each dictionary of the input_list

Return type:

list of python dictionaries

tnbs.BTC_02_AE.QQuantLib.utils.benchmark_utils.create_ae_pe_solution(ae_list, problem_list)

Creates a list of price estimation problems for solving with amplitude estimation (AE) techniques. Each element will have the complete information for generating a price estimation problem and the configuration for solving it using an AE algorithm. This is each element is a python dictionary that allows define a price estimation problem and solving it using a properly configure AE algorithm

Parameters:
  • ae_list (list) – List with properly configured AE solvers.

  • problem_list (list) – List with different price estimation problems.

Returns:

solve_ae_pe_list – List where each element is a ae_pricep dictionary The list will have the combination of each posible amplitude estimation solver with all posible price problem list

Return type:

list

tnbs.BTC_02_AE.QQuantLib.utils.benchmark_utils.create_pe_problem(domain_cfg, payoff_cfg, density_cfg)

Create a list of price estimation problems. Each element is a python dictionary with a complete option price estimation problem.

Parameters:
  • domain_cfg (list of dictionaries) – Each dictionary has a domain configuration for a price estimation problem.

  • payoffs_cfg (list of dictionaries) – Each dictionary has an option configuration for a price estimation problem.

  • density_cfg (list of dictionaries) – Each dictionary has probability density configuration for a price estimation problem.

Returns:

pe_problem_list – list with different price estimation problems.

Return type:

list of dictionaries

tnbs.BTC_02_AE.QQuantLib.utils.benchmark_utils.list_of_dicts_from_jsons(ae_json_list)

Creates a list of dictionaries from inputs jsons.

Parameters:

ae_list (list of json.) – List with name of json files with a complete configuration of an amplitude estimation method

Returns:

ae_pricep_list

Return type:

list of python dictionaries

QQuantLib.utils.data_extracting

This module contains auxiliary functions for executing QLM programs based on QLM Routines or QLM gates and for post processing results from QLM qpu executions

Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro Costas

tnbs.BTC_02_AE.QQuantLib.utils.data_extracting.create_qcircuit(prog_q)

Given a QLM program creates a QLM circuit

Parameters:

prog_q (QLM QProgram)

Returns:

circuit

Return type:

QLM circuit

tnbs.BTC_02_AE.QQuantLib.utils.data_extracting.create_qjob(circuit, shots=0, qubits=None)

Given a QLM circuit creates a QLM job

Parameters:
  • circuit (QLM circuit)

  • shots (int) – number of measurmentes

  • qubits (list) – with the qubits to be measured

Returns:

job – job for submit to QLM QPU

Return type:

QLM job

tnbs.BTC_02_AE.QQuantLib.utils.data_extracting.create_qprogram(quantum_gate)

Creates a Quantum Program from an input qlm gate or routine

Parameters:

quantum_gate (QLM gate or QLM routine)

Returns:

q_prog – Quantum Program from input QLM gate or routine

Return type:

QLM Program.

tnbs.BTC_02_AE.QQuantLib.utils.data_extracting.get_results(quantum_object, linalg_qpu, shots: int = 0, qubits: list = None, complete: bool = False)

Function for testing an input gate. This function creates the quantum program for an input gate, the correspondent circuit and job. Execute the job and gets the results

Parameters:
  • quantum_object (QLM Gate, Routine or Program)

  • linalg_qpu (QLM solver)

  • shots (int) – number of shots for the generated job. if 0 True probabilities will be computed

  • qubits (list) – list with the qubits for doing the measurement when simulating if None measurement over all allocated qubits will be provided

  • complete (bool) – for return the complete basis state. Useful when shots is not 0 and all the posible basis states are necessary.

Returns:

  • pdf (pandas DataFrame) – DataFrame with the results of the simulation

  • circuit (QLM circuit)

  • q_prog (QLM Program.)

  • job (QLM job)

tnbs.BTC_02_AE.QQuantLib.utils.data_extracting.proccess_qresults(result, qubits, complete=False)

Post Process a QLM results for creating a pandas DataFrame

Parameters:
  • result (QLM results from a QLM qpu.) – returned object from a qpu submit

  • qubits (int) – number of qubits

  • complete (bool) – for return the complete basis state.

QQuantLib.utils.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

tnbs.BTC_02_AE.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

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

Raise an exception if variable is None