Gates

Experiments

Experiments on level gate. Generates the results which are to be visualized.

pulse_opt.gates.experiments.simulate_gate(GateFactoryClass: type[pulse_opt.gates.factories.GateFactory], gate_args: dict, pulse_lookup: dict, run: str, samples: int = 10000, runs: int = 50, prefix: str = 'X')[source]

Compute a gate for a specific level of noise and various pulses to a high precision.

Parameters:
  • GateFactoryClass (type[GateFactory]) –

  • gate_args (dict) – Arguments like theta, T1, p_cnot that are used to construct the gate stored as lookup.

  • pulse_lookup (str) – Lookup with the pulse name or parametrization value as key and the pulse as value.

  • run (str) – Name of the current run of the experiment.

  • samples (int) – Number of gates to be sampled for each run.

  • runs (int) – Number of repetitions of the experiments.

  • prefix (str) – Gate name or prefix to be added to the filenames of the results.

pulse_opt.gates.experiments._gate_experiment_with_single_argument(args)[source]

Wrapper to the _gate_experiment() function to be able to call the function with a single argument.

The fact that this method has just a single argument is necessary for using the multiprocesing.map_unordered method.

Parameters:
  • args (dict) – All the arguments of the gate experiment saved as a lookup, where the key is the argument name

  • type. (as str and the value is the argument value in the corresponding) –

pulse_opt.gates.experiments._gate_experiment(pulse_lookup: dict, GateFactoryClass: type[pulse_opt.gates.factories.GateFactory], gate_args: dict, samples: int = 10000)[source]

Samples n gates with the GateFactoryClass for each pulses defined in the lookup.

Uses the noise parameters given in the gate_args lookup.

Parameters:
  • pulse_lookup (str) – Lookup with the pulse name or parametrization value as key and the pulse as value.

  • GateFactoryClass (type[GateFactory]) –

  • gate_args (dict) – Arguments like theta, T1, p_cnot that are used to construct the gate stored as lookup.

  • run (str) – Name of the current run of the experiment.

  • samples (int) – Number of gates to be sampled for each run.

  • runs (int) – Number of repetitions of the experiments.

  • prefix (str) – Gate name or prefix to be added to the filenames of the results.

Returns:

A lookup of the results with the name of the pulses as key (str) and with values being lookups itself. Each of these lookup has mean, std, and std_sqrt(n) as keys, and arrays as values. More precicely: mean: np.array with mean of the sampled gates Mean of the population std: np.array with standard deviation of the sampled gates Empirical standard deviation of the population std_sqrt(n): np.array with uncertainty of the mean of the sampled gates Empirical uncertainty of the mean of the population.

pulse_opt.gates.experiments._reshape_gate(gate: array) array[source]

Takes a complex 2x2 (4x4) array and turns it to a vector with 8 (32) real entries, which represent the real and complex part of the array.

Example input:

np.array([[1, J],[0, 0])

Example output:

np.array([1, 0, 0, 0, 0, 1, 0, 0])

Parameters:

gate (np.array) – Complex 2x2 (4x4) array

Returns:

Vector with 8 (32) real entries, which represent the real and complex part of the array.

Return type:

vector (np.array)

Factories

Factories for creating gates with just a function call without any extra arguments.

This allows us to give the arguments upon initialization. Note that The library quantum-gates specifies similar classes with names XFactory, CNOTFactory, etc. These factories

do not support default arguments.

pulse_opt.gates.factories.factory_class_lookup[source]

Lookup with the gate name (str) as key and the corresponding factory class (GateFactory) as value.

Type:

dict

class pulse_opt.gates.factories.GateFactory(pulse: Pulse, gate_args: dict)[source]

Creates gates with a argument-free function call.

Parameters:
  • pulse (Pulse) –

  • gate_args (dict) –

pulse[source]

Pulse used in the gates (Gates).

Type:

Pulse

gate_args[source]

Arguments that are used to sample the gates. These are the inputs of the construct() method in the Gates class.

Type:

dict

abstract construct() array[source]

Samples a gate as specified by the attributes.

Abstract method which is overwritten in the child classes to sample a specific gate.

Returns:

The sampled gate.

Return type:

np.array

class pulse_opt.gates.factories.XGateFactory(pulse: Pulse, gate_args)[source]

Creates an X gate with a argument-free function call.

Parameters:

pulse (Pulse) –

pulse[source]

Pulse used in the gates (Gates).

Type:

Pulse

gate_args[source]

Arguments that are used to sample the gates. These are the inputs of the construct() method in the Gates class.

Type:

dict

construct() array[source]

Samples an X gate as specified by the attributes.

Returns:

The sampled gate.

Return type:

np.array

class pulse_opt.gates.factories.SXGateFactory(pulse: Pulse, gate_args)[source]

Creates an SX gate with a argument-free function call.

Parameters:

pulse (Pulse) –

pulse[source]

Pulse used in the gates (Gates).

Type:

Pulse

gate_args[source]

Arguments that are used to sample the gates. These are the inputs of the construct() method in the Gates class.

Type:

dict

construct() array[source]

Samples a SX gate as specified by the attributes.

Returns:

The sampled gate.

Return type:

np.array

class pulse_opt.gates.factories.CRGateFactory(pulse: Pulse, gate_args)[source]

Creates a CR gate with a argument-free function call.

Parameters:

pulse (Pulse) –

Return type:

array

pulse[source]

Pulse used in the gates (Gates).

Type:

Pulse

gate_args[source]

Arguments that are used to sample the gates. These are the inputs of the construct() method in the Gates class.

Type:

dict

construct()[source]

Samples a CR gate as specified by the attributes.

Returns:

The sampled gate.

Return type:

np.array

class pulse_opt.gates.factories.CNOTGateFactory(pulse: Pulse, gate_args)[source]

Creates a CNOT gate with a argument-free function call.

Parameters:

pulse (Pulse) –

Return type:

array

pulse[source]

Pulse used in the gates (Gates).

Type:

Pulse

gate_args[source]

Arguments that are used to sample the gates. These are the inputs of the construct() method in the Gates class.

Type:

dict

construct()[source]

Samples a CNOT gate as specified by the attributes.

Returns:

The sampled gate.

Return type:

np.array

class pulse_opt.gates.factories.CNOTInvGateFactory(pulse: Pulse, gate_args)[source]

Creates an CNOT inverse gate with a argument-free function call.

Parameters:

pulse (Pulse) –

Return type:

array

pulse[source]

Pulse used in the gates (Gates).

Type:

Pulse

gate_args[source]

Arguments that are used to sample the gates. These are the inputs of the construct() method in the Gates class.

Type:

dict

construct()[source]

Samples a CNOT inverse gate as specified by the attributes.

Returns:

The sampled gate.

Return type:

np.array

Visualizations

Utilities

Utilities for simulating the gates.

pulse_opt.gates.utilities.result_metrics[source]

List of the metrics we compute on the list of sampled gates.

Type:

list[str]

pulse_opt.gates.utilities.aggregated_metrics[source]

List of the metrics we compute on the list of result metrics.

Type:

list[str]

pulse_opt.gates.utilities.gate_args_constructor_lookup[source]

Lookup with the gate name as key and the constructor of the gate arguments as value.

Type:

dict

pulse_opt.gates.utilities.construct_x_gate_args(device_param_lookup: dict, noise_scaling: float = 1.0, phi: float = 0.0) dict[source]

Constructs the arguments used to sample an X or SX gate.

Parameters:
  • device_param_lookup (dict) – DeviceParameters as lookup. Contains the information about the level of noise.

  • noise_scaling (float) – Value by which the level of noise specified in the device parameters is scaled.

  • phi (float) – Phase of the X gate.

Returns:

Lookup of the argument names (str) as key and the argument (float) as value.

Return type:

dict

pulse_opt.gates.utilities.construct_cnot_gate_args(device_param_lookup: dict, noise_scaling: float = 1.0, phi_ctr: float = 0.0, phi_trg: float = 0.0) dict[source]

Constructs the arguments used to sample an CNOT or CNOT inv gate.

Parameters:
  • device_param_lookup (dict) – DeviceParameters as lookup. Contains the information about the level of noise.

  • noise_scaling (float) – Value by which the level of noise specified in the device parameters is scaled.

  • phi_ctr (float) – Phase of the control qubit.

  • phi_trg (float) – Phase of the target qubit.

Returns:

Lookup of the argument names (str) as key and the argument (float) as value.

Return type:

dict

pulse_opt.gates.utilities.construct_cr_gate_args(device_param_lookup: dict, noise_scaling: float = 1.0, theta: float = 0.7853981633974483, phi: float = 0.0) dict[source]

Constructs the arguments used to sample a CR gate.

Parameters:
  • device_param_lookup (dict) – DeviceParameters as lookup. Contains the information about the level of noise.

  • noise_scaling (float) – Value by which the level of noise specified in the device parameters is scaled.

  • theta (float) – Angle of rotation.

  • phi (float) – Phase.

Returns:

Lookup of the argument names (str) as key and the argument (float) as value.

Return type:

dict

pulse_opt.gates.utilities.perform_parallel_simulation(args: list, simulation: callable, max_workers: int = 2) list[source]

Wrapper to the multiprocessing.imap_unordered method.

The arguments are mapped with the simulation by a maximum number of workers. Note that the ordering of the results is not guaranteed to correspond to the ordering of the arguments.

Parameters:
  • args (list) – List of the arguments which are passed to the simulation.

  • simulation (callable) – Function to be applied on each item of args. Must have a single argument.

  • max_workers (int) – Maximum number of multiprocessing pool workers to be created.

Returns:

List of the return values of the simulation, one for each argument, but in arbitrary ordering.

Return type:

list

pulse_opt.gates.utilities.perform_trivial_simulation(args: list, simulation: callable, max_workers: int = 2) list[source]

Mock version of perform_parallel_simulation.

This version uses a trivial for loop and is meant for debugging.

Parameters:
  • args (list) – List of the arguments which are passed to the simulation.

  • simulation (callable) – Function to be applied on each item of args. Must have a single argument.

  • max_workers (int) – Mock argument such that the interface is the same.

Returns:

List of the return values of the simulation, one for each argument, but in arbitrary ordering.

Return type:

list

pulse_opt.gates.utilities.save_results(results: list, folder: str, prefix: str)[source]

Takes results in the form of a lookup and saves it to a folder with filenames that have a certain prefix.

Example input:

results = [{ “pulse_0.0”: {“mean”: np.array([0.0,…,0.0]), …, “std_sqrt(n)”: np.array([0.0,…,0.0])}, … “pulse_1.0”: {“mean”: np.array([0.0,…,0.0]), …, “std_sqrt(n)”: np.array([0.0,…,0.0])}, } for i in range(10)] folder = “abc” prefix = “trivial”

Example action:

The method will create text files in the folder with filenames trivial_mean_pulse_0.0_0.txt … trivial_unc_pulse_1.0_9.txt

Parameters:
  • results (list[dict]) – Results as produced by the simulate_gate function in experiments.py.

  • folder (str) – Path and name of the folder in which the results should be saved.

  • prefix (str) – Prefix to be added to the names of the files. Must not contain any underscores (_).

pulse_opt.gates.utilities.save_aggregated_results(result: dict, folder: str, prefix: str)[source]

Does the same as save_results(), but for a single lookup table which was created with aggregation.

Parameters:
  • results (list[dict]) – Results as produced by the simulate_gate function in experiments.py.

  • folder (str) – Path and name of the folder in which the results should be saved.

  • prefix (str) – Prefix to be added to the names of the files. Must not contain any underscores (_).

  • result (dict) –

pulse_opt.gates.utilities.aggregate_results(results: list)[source]

Aggregates a list of lookup tables, which contain lookup tables with keys (mean, std, std_sqrt(n)) on their own.

Example input:

results = [{ “pulse_0.0”: {“mean”: np.array([0.0,…,0.0]), …, “std_sqrt(n) “: np.array([0.0,…,0.0])}, … “pulse_1.0”: {“mean”: np.array([0.0,…,0.0]), …, “std_sqrt(n)”: np.array([0.0,…,0.0])}, } for i in range(10)]

Example output:

output = { “pulse_0.0”: {“mean(mean)”: np.array([0.0,…,0.0]), …, “std(mean) over sqrt(n)”: np.array([0.0,…,0.0])}, … “pulse_1.0”: {“mean(mean)”: np.array([0.0,…,0.0]), …, “std(mean) over sqrt(n)”: np.array([0.0,…,0.0])}, }

Returns:

A single lookup table with the result as aggregated across the lookups. Namely, we aggregate

mean(mean) Mean of the population mean(std) Empirical standard deviation of the population. std(mean) Empirical standard deviation of the mean estimator std(std) Empirical standard deviation of the std estimator std(mean) over sqrt(n) Empirical uncertainty of the mean of means

Parameters:

results (list) –

pulse_opt.gates.utilities.load_results(folder: str) list[source]

Inverse method of save_results.

Parses the results file generated from a run and returns the results in the original lookup format.

Parameters:

folder (str) – Path and name of the folder in which the files were saved by the save_results() function.

Return type:

list

pulse_opt.gates.utilities.load_aggregated_results(folder: str) list[source]

Inverse method of save_aggregated_results.

Parses the aggregated results files generated from a run and returns them in the original lookup format.

Parameters:

folder (str) – Path and name of the folder in which the files were saved by the save_aggregated_results() function.

Return type:

list

pulse_opt.gates.utilities.hellinger_distance(p1: array, p2: array) float[source]

Compute the Hellinger distance between to probability distributions.

Parameters:
  • p1 (np.array) – Non-negative array with sum equal to 1.

  • p2 (np.array) – Non-negative array with sum equal to 1, must have the same shape as p1.

Returns:

Hellinger distance H(p1, p2) as float.

Return type:

float

pulse_opt.gates.utilities.u_hellinger_distance(u_arr1, u_arr2) umatrix[source]

Compute the Hellinger distance between to probability distributions with uncertainties.

Parameters:
  • u_arr1 (unumpy.matrix) – Non-negative array with sum equal to 1 and uncertainties.

  • u_arr2 (unumpy.matrix) – Non-negative array with sum equal to 1 and uncertainties, must have the same shape.

Returns:

Hellinger distance H(u_arr1, u_arr2) as unumpy.umatrix containing the value with the uncertainty.

Return type:

umatrix

pulse_opt.gates.utilities.u_sqrt(u_arr: umatrix) umatrix[source]

Element-wise square root of an unumpy array.

Parameters:

u_arr (unumpy.umatrix) – Matrix

Returns:

Matrix (unumpy.umatrix).

Return type:

umatrix