Package: qubit-approximant
QubitApproximantApythonpackage for approximating quantum circuits with a single qubit.Documentation and examplesDocumentation created withmkdocscan be found inhttps://pablovegan.github.io/QubitApproximant/.InstallationWithpip:pip install qubit-approximantQuick usageImporting a functionIn the submodule benchmarking.functions there are multiple test functions to choose fromimportnumpyasnpfromqubit_approximant.benchmarking.functionsimportgaussianx=np.linspace(-2.5,2.5,1000)fn_kwargs={'mean':0.0,'std':0.5,'coef':1}fn=gaussian(x,**fn_kwargs)Creating a circuitTo create a circuit just choose the ansaz (CircuitRxRyRz,CircuitRxRyorCircuitRy) and the encoding ('prob'or'amp').fromqubit_approximant.coreimportCircuitRxRyRzcircuit=CircuitRxRyRz(x,encoding='prob')Cost functionTo find the optimum parameters of the circuit, we need to choose a cost function. This can be done with theCostclass, where we input the function to approximate, the circuit ansatz and a metric to quantify the error in the approximation (options are'mse','rmse','mse_weighted','kl_divergence'or'log_cosh')fromqubit_approximant.coreimportCostcost=Cost(fn,circuit,metric='mse')OptimizerChoose an optimizer (BlackBoxOptimizer,GDOptimizerorAdamOptimizer)fromqubit_approximant.coreimportBlackBoxOptimizeroptimizer=BlackBoxOptimizer(method="L-BFGS-B")and find the optimum parameters for the chosen circuitlayers=6init_params=np.random.default_rng().standard_normal(4*layers)opt_params=optimizer(cost,cost.grad,init_params)Multilayer optimizerWe may also optimize an ansatz for multiple layers using theLayerwiseOptimizer, which uses the optimum parameters for a circuit with $L$ layers as initial parameters for the optimization of a circuit with $L+1$ layers. A list with the optimum parameters for each layer is returned.fromqubit_approximant.coreimportLayerwiseOptimizerlayerwise_opt=LayerwiseOptimizer(optimizer,min_layer=3,max_layer=7,new_layer_coef=0.3,new_layer_position='random')params_list=layerwise_opt(cost,cost.grad,init_params)Note: aMultilayerOptimizerwhich doesn't reuse the optimized parameters from previous layers is also available.Error metricsTo benchmark the optimization we can use some common metrics, like the $L^1$ norm, $L^2$ norm, $L^\infty$ norm or infidelity $1-F$, to compare the function encoded in the circuit with the desired function. Following our example,fnis agaussian:l1_list,l2_list,inf_list,infidelity_list=metric_results(params_list,circuit,fn=gaussian,fn_kwargs={'mean':0.0,'std':0.5,'coef':1})Wrapping upTest the library yourself!importnumpyasnpfromqubit_approximant.benchmarking.functionsimportgaussianfromqubit_approximant.coreimportCircuitRxRyRz,Cost,BlackBoxOptimizer,LayerwiseOptimizerfromqubit_approximant.benchmarkingimportmetric_resultsx=np.linspace(-2.5,2.5,1000)fn_kwargs={'mean':0.0,'std':0.5,'coef':1}fn=gaussian(x,**fn_kwargs)circuit=CircuitRxRyRz(x,encoding='prob')cost=Cost(fn,circuit,metric='mse')optimizer=BlackBoxOptimizer(method="L-BFGS-B")min_layer=3init_params=np.random.default_rng().standard_normal(4*min_layer)layerwise_opt=LayerwiseOptimizer(optimizer,min_layer=min_layer,max_layer=7,new_layer_coef=0.3,new_layer_position='random')params_list=layerwise_opt(cost,cost.grad,init_params)l1_list,l2_list,inf_list,infidelity_list=metric_results(fn=gaussian,fn_kwargs={'mean':0.0,'std':0.5,'coef':1},circuit=circuit,params_list=params_list)Bonus: benchmarking multiple initial parametersThe initial paramenters for the optimizer are generated at random with aseedof our choice. We can benchmark the optimizer against multiple seeds (since it is a time consuming task it is parallelized usingmpi).benchmark_seeds(num_seeds=4,fn=gaussian,fn_kwargs=fn_kwargs,circuit=circuit,cost=cost,optimizer=multilayer_opt,filename="results",)ReferencesThis library is based on Adrian Pérez Salinas articleData re-uploading for a universal quantum classifier.ContributingPull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.Please make sure to update tests as appropriate.LicenseThis software is under theGNU General Public License v3.0.