Skip to content

Commit

Permalink
Added function for saving stuff in load_probabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfeco committed May 24, 2024
1 parent 6858c35 commit 56a9724
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 35 deletions.
83 changes: 63 additions & 20 deletions tnbs/BTC_01_PL/PL/load_probabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@
from scipy.stats import entropy, kstest
from data_loading import get_theoric_probability, get_qlm_probability

def save(pl_object, folder, name=None):
"""
Function for saving staff
"""

pdf_filename = folder + "pl_result_nqbits_"\
+ str(pl_object.n_qbits) + name + ".csv"
# Save the result data frame
pl_object.pdf.to_csv(pdf_filename, sep=";")
# Save the probability distribution
pdf_filename = folder + "pl_probability_nqbits_"\
+ str(pl_object.n_qbits) + name + ".csv"
pl_object.kl_pdf.to_csv(pdf_filename, sep=";")
try:
from qat.interop.qiskit import qlm_to_qiskit
# Transform to qiskit circuit
qlm_circuit = pl_object.circuit
circuit_qiskit = qlm_to_qiskit(qlm_circuit)
qasm_filename = folder + "pl_circuit_nqbits_"\
+ str(pl_object.n_qbits) + name + ".qasm"
circuit_qiskit.qasm(filename = qasm_filename)
except ImportError:
print("Circuit WILL NOT BE STORED. ImportError problem")


class LoadProbabilityDensity:
"""
Probability Loading
Expand Down Expand Up @@ -37,7 +62,7 @@ def __init__(self, **kwargs):
if self.qpu is None:
error_text = "Please provide a QPU."
raise ValueError(error_text)

self.data = None
self.p_gate = None
self.result = None
Expand Down Expand Up @@ -155,6 +180,7 @@ def summary(self):
if __name__ == "__main__":
import argparse
import json
import os
from qpu.benchmark_utils import combination_for_list
from qpu.select_qpu import select_qpu

Expand All @@ -175,13 +201,40 @@ def summary(self):
help="For selecting the load method: multiplexor, brute_force, KPTree",
default=None,
)
#QPU argument
parser.add_argument(
"-qpu",
dest="qpu",
"-json_qpu",
dest="json_qpu",
type=str,
default="qpu/qpu.json",
help="JSON with the qpu configuration",
)
parser.add_argument(
"-id",
dest="id",
type=int,
help="For executing only one element of the list",
default=None,
)
parser.add_argument(
"-name",
dest="base_name",
type=str,
help="Additional name for the generated files. Only with --save",
default="",
)
parser.add_argument(
"-folder",
dest="folder",
type=str,
default="python",
help="QPU for simulation: See function get_qpu in get_qpu module",
help="Path for storing folder. Only with --save",
default="./",
)
parser.add_argument(
"--save",
dest="save",
default=False,
action="store_true",
help="For saving staff"
)
parser.add_argument(
"--count",
Expand All @@ -197,20 +250,6 @@ def summary(self):
action="store_true",
help="For printing "
)
parser.add_argument(
"-id",
dest="id",
type=int,
help="For executing only one element of the list",
default=None,
)
parser.add_argument(
"-json_qpu",
dest="json_qpu",
type=str,
default="qpu/qpu.json",
help="JSON with the qpu configuration",
)
parser.add_argument(
"--exe",
dest="execution",
Expand Down Expand Up @@ -246,3 +285,7 @@ def summary(self):
prob_dens = LoadProbabilityDensity(**configuration)
prob_dens.exe()
print(prob_dens.pdf)
if args.save:
if not os.path.exists(args.folder):
os.mkdir(args.folder)
save(prob_dens, args.folder, args.base_name)
80 changes: 65 additions & 15 deletions tnbs/BTC_01_PL/PL/notebooks/01_ProbabilityLoadingKernel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -763,32 +763,82 @@
"source": [
"### Command line execution\n",
"\n",
"The complete **BTC** can be executed by invoking the module **PL/load_probabilities** as a command line. For getting the inputs arguments the following command can be used:\n",
"The complete **BTC** can be executed by invoking the module **PL/load_probabilities** as a command line. For getting the input arguments the following command can be used:\n",
"\n",
" python load_probabilities.py -h\n",
"\n",
" usage: load_probabilities.py [-h] [-n_qbits N_QBITS] [-method METHOD] [-qpu QPU] [--count] [--print] [-id ID] [-json_qpu JSON_QPU]\n",
" usage: load_probabilities.py [-h] [-n_qbits N_QBITS] [-method METHOD] [-json_qpu JSON_QPU] [-id ID] [-name BASE_NAME] [-folder FOLDER] [--save] [--count] [--print]\n",
" [--exe]\n",
"\n",
" optional arguments:\n",
" options:\n",
" -h, --help show this help message and exit\n",
" -n_qbits N_QBITS Number of qbits for interval discretization.\n",
" -method METHOD For selecting the load method: multiplexor, brute_force, KPTree\n",
" -qpu QPU QPU for simulation: See function get_qpu in get_qpu module\n",
" -json_qpu JSON_QPU JSON with the qpu configuration\n",
" -id ID For executing only one element of the list\n",
" -name BASE_NAME Additional name for the generated files. Only with --save\n",
" -folder FOLDER Path for storing folder. Only with --save\n",
" --save For saving staff\n",
" --count For counting elements on the list\n",
" --print For printing\n",
" -id ID For executing only one element of the list\n",
" -json_qpu JSON_QPU JSON with the qpu configuration\n",
" --exe For executing program\n",
"\n",
"The qpu configuration should be provided as a JSON file. In the folder **PL/qpu** two examples of JSON files can be found:\n",
"* *PL/qpu/qpu_ideal.json*: This JSON configures qpus for ideal simulation.\n",
"* *PL/qpu/qpu_noisy.json*: This JSON configures qpus for noisy simulation (only valid if the user is connected to an EVIDEN QLM)\n",
"\n",
"These JSON files allow to the user configure several qpus at the same time and the user can select which one to use. \n",
"\n",
"\n",
"#### **--count** argument\n",
"\n",
"The **--count** argument allows to the user know how many qpus have been configured for the corresponding JSON qpu file configuration. \n",
"\n",
"If the *PL/qpu/qpu_ideal.json* was not modified then the following command will return 6 (because 6 different qpus are configured originally in the JSON file):\n",
"\n",
" python load_probabilities.py -n_qbits 10 -method multiplexor -json_qpu qpu/qpu_ideal.json --count\n",
"\n",
"\n",
"#### **--print** argument\n",
"\n",
"The **--print** argument in combination with the -id ID argument allows to the user know what is the configuration of the QPU, the number of qubits and the method that will be used.\n",
"\n",
"If the *PL/qpu/qpu_ideal.json* was not modified then the following command:\n",
"\n",
"The qpu configuration should be provided as json file. In the folder **PL/qpu** two josn samples can be found:\n",
"* *PL/qpu/qpu_ideal.json*: this json configures qpus for ideal simulation.\n",
"* *PL/qpu/qpu_noisy.json*: this json configures qpus for noisy simulation (only valid if user is connected to a EVIDEN QLM)\n",
" python load_probabilities.py -n_qbits 10 -method multiplexor -json_qpu qpu/qpu_ideal.json -id 0 --print\n",
"\n",
"Example: for a 10 qubits circuit using *KPTree* the following command can be used:\n",
"will return:\n",
"\n",
" python load_probabilities.py -n_qbits 8 -method KPTree -json qpu/qpu_ideal.json -id 0 --exe\n",
" {'load_method': 'multiplexor', 'number_of_qbits': 10, 'qpu': {'qpu_type': 'c', 't_gate_1qb': None, 't_gate_2qbs': None, 't_readout': None, 'depol_channel': {'active': False, 'error_gate_1qb': None, 'error_gate_2qbs': None}, 'idle': {'amplitude_damping': False, 'dephasing_channel': False, 't1': None, 't2': None}, 'meas': {'active': False, 'readout_error': None}}}\n",
" \n",
"In this case the *CLinalg* myqlm qpu is used (if the qpu/qpu_ideal.json is not modified).\n"
" \n",
"Meanwhile the command \n",
"\n",
" python load_probabilities.py -n_qbits 10 -method multiplexor -json_qpu qpu/qpu_ideal.json -id 3 --print\n",
"\n",
"will return:\n",
"\n",
" {'load_method': 'multiplexor', 'number_of_qbits': 10, 'qpu': {'qpu_type': 'mps', 't_gate_1qb': None, 't_gate_2qbs': None, 't_readout': None, 'depol_channel': {'active': False, 'error_gate_1qb': None, 'error_gate_2qbs': None}, 'idle': {'amplitude_damping': False, 'dephasing_channel': False, 't1': None, 't2': None}, 'meas': {'active': False, 'readout_error': None}}}\n",
" \n",
" \n",
"#### **--exe** argument\n",
"\n",
"The **--exe** argument in combination with the -id ID argument allows the user to solve the desired probability loading problem with the selected qpu. \n",
"\n",
"If the *PL/qpu/qpu_ideal.json* was not modified then the following command:\n",
"\n",
" python load_probabilities.py -n_qbits 10 -method multiplexor -json_qpu qpu/qpu_ideal.json -id 0 --exe\n",
" \n",
"will solver the **PL** for a 10 qubits probability density discretization, using the **multiplexor** methods for building the $\\mathbf{U}_p$ operator and the **CLinalg** qpu.\n",
"\n",
"#### **--save** argument\n",
"\n",
"The **--save** argument, in combination with -id ID and the **--exe** arguments, will execute the PL and save the following staff:\n",
"* The summary pandas DataFrame (*pdf* attribute of the object)\n",
"* The probability pandas DataFrame (*kl_pdf* attribute of the object)\n",
"* The qiskit export of the quantum circuit (*circuit* attribute of the object as qisikit circuit)\n",
"\n",
"The folder for storing can be provided using the *-folder FOLDER* and with *-name BASE_NAME* the user can provide a name to add to the different generated files."
]
},
{
Expand Down Expand Up @@ -834,9 +884,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "myqlm_tes",
"language": "python",
"name": "python3"
"name": "myqlm_tes"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -848,7 +898,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 56a9724

Please sign in to comment.