Package: qiskit-symb
Qiskit DemoDayspresentation $\rightarrow$ :link:link,
password:Demoday20230615(15th June 2023)Jupyter notebook demo $\rightarrow$ :link:link(15th June 2023)Qiskit Medium blog $\rightarrow$ :link:link(28th June 2023)Table of contentsIntroductionInstallationUser-modeDev-modeUsage examplesSympifya Qiskit circuitLambdifya Qiskit circuitContributorsIntroductionTheqiskit-symbpackage is meant to be a Python tool to enable the symbolic evaluation of parametric quantum states and operators defined inQiskitby parameterized quantum circuits.A Parameterized Quantum Circuit (PQC) is a quantum circuit where we have at least one free parameter (e.g. a rotation angle $\theta$). PQCs are particularly relevant in Quantum Machine Learning (QML) models, where the values of these parameters can be learned during training to reach the desired output.In particular,qiskit-symbcan be used to create a symbolic representation of a parametric quantum statevector, density matrix, or unitary operator directly from the Qiskit quantum circuit. This has been achieved through the re-implementation of some basic classes defined in theqiskit/quantum_info/module by usingsympyas a backend for symbolic expressions manipulation.InstallationUser-modepip install qiskit-symb:warning: The package requiresqiskit>=1. See the officialMigration guidesif you are used to a prevoius Qiskit version.Dev-modegit clone https://github.com/SimoneGasperini/qiskit-symb.git
cd qiskit-symb
pip install -e .Usage examplesSympifya Qiskit circuitLet's get started on how to useqiskit-symbto get the symbolic representation of a given Qiskit circuit. In particular, in this first basic example, we consider the following quantum circuit:fromqiskitimportQuantumCircuitfromqiskit.circuitimportParameter,ParameterVectory=Parameter('y')p=ParameterVector('p',length=2)pqc=QuantumCircuit(2)pqc.ry(y,0)pqc.cx(0,1)pqc.u(0,*p,1)pqc.draw('mpl')To get thesympyrepresentation of the unitary matrix corresponding to the parameterized circuit, we just have to create the symbolicOperatorinstance and call theto_sympy()method:fromqiskit_symb.quantum_infoimportOperatorop=Operator(pqc)op.to_sympy()\left[\begin{matrix}\cos{\left(\frac{y}{2} \right)} & - \sin{\left(\frac{y}{2} \right)} & 0 & 0\\0 & 0 & \sin{\left(\frac{y}{2} \right)} & \cos{\left(\frac{y}{2} \right)}\\0 & 0 & e^{i \left(p[0] + p[1]\right)} \cos{\left(\frac{y}{2} \right)} & - e^{i \left(p[0] + p[1]\right)} \sin{\left(\frac{y}{2} \right)}\\e^{i \left(p[0] + p[1]\right)} \sin{\left(\frac{y}{2} \right)} & e^{i \left(p[0] + p[1]\right)} \cos{\left(\frac{y}{2} \right)} & 0 & 0\end{matrix}\right]If you want then to assign a value to some specific parameter, you can use thesubs(<dict>)method passing a dictionary that maps each parameter to the desired corresponding value:new_op=op.subs({p:[-1,2]})new_op.to_sympy()\left[\begin{matrix}\cos{\left(\frac{y}{2} \right)} & - \sin{\left(\frac{y}{2} \right)} & 0 & 0\\0 & 0 & \sin{\left(\frac{y}{2} \right)} & \cos{\left(\frac{y}{2} \right)}\\0 & 0 & e^{i} \cos{\left(\frac{y}{2} \right)} & - e^{i} \sin{\left(\frac{y}{2} \right)}\\e^{i} \sin{\left(\frac{y}{2} \right)} & e^{i} \cos{\left(\frac{y}{2} \right)} & 0 & 0\end{matrix}\right]Lambdifya Qiskit circuitGiven a Qiskit circuit,qiskit-symbalso allows to generate a Python lambda function with actual arguments matching the Qiskit unbound parameters.
Let's consider the following example starting from aZZFeatureMapcircuit, commonly used as a data embedding ansatz in QML applications:fromqiskit.circuit.libraryimportZZFeatureMappqc=ZZFeatureMap(feature_dimension=3,reps=1)pqc.draw('mpl')To get the Python function representing the final parameteric statevector, we just have to create the symbolicStatevectorinstance and call theto_lambda()method:fromqiskit_symb.quantum_infoimportStatevectorpqc=pqc.decompose()statevec=Statevector(pqc).to_lambda()We can now call the lambda-generated functionstatevecpassing thexvalues we want to assign to each parameter. The returned object will be anumpy2D-array (withshape=(8,1)in this case) representing the final output statevectorpsi.x=[1.24,2.27,0.29]psi=statevec(*x)This feature can be useful when, given a Qiskit PQC, we want to run it multiple times with different parameters values. Indeed, we can perform a single symbolic evalutation and then call the lambda generated function as many times as needed, passing different values of the parameters at each iteration.ContributorsSimone Gasperini