Skip to main content

Symbolic evaluation of parameterized quantum circuits in Qiskit

Project description

Table of contents

Introduction

The qiskit-symb package is meant to be a Python tool to enable the symbolic evaluation of parametric quantum states defined by Qiskit by 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-symb can be used to create a symbolic representation of a parametric quantum state directly from the Qiskit quantum circuit. This has been achieved through the re-implementation of some basic classes defined in the qiskit/quantum_info/ module by using sympy as a backend for symbolic expressions manipulation.

Installation

User-mode

pip install qiskit-symb

Dev-mode

git clone https://github.com/SimoneGasperini/qiskit-symb.git
cd qiskit-symb
pip install requirements-dev.txt
pip install -e .

Usage examples

Sympify a Qiskit circuit

Let's get started on how to use qiskit-symb to get the symbolic representation of a given Qiskit circuit. In particular, in this first basic example, we consider the following quantum circuit:

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter, ParameterVector

y = Parameter('y')
p = ParameterVector('p', length=2)

pqc = QuantumCircuit(2)
pqc.ry(y, 0)
pqc.cx(0, 1)
pqc.u(p[0], 0, p[1], 1)

pqc.draw('mpl')

To get the sympy representation of the unit-norm complex vector prepared by the parameterized circuit, we just have to create the symbolic Statevector instance and call the to_sympy() method:

from qiskit_symb.quantum_info import Statevector

psi = Statevector(pqc)
psi.to_sympy()
\left[\begin{matrix}\cos{\left(\frac{p[0]}{2} \right)} \cos{\left(\frac{y}{2} \right)} & - e^{1.0 i p[1]} \sin{\left(\frac{p[0]}{2} \right)} \sin{\left(\frac{y}{2} \right)} & \sin{\left(\frac{p[0]}{2} \right)} \cos{\left(\frac{y}{2} \right)} & e^{1.0 i p[1]} \sin{\left(\frac{y}{2} \right)} \cos{\left(\frac{p[0]}{2} \right)}\end{matrix}\right]

If you want then to assign a value to some specific parameter, you can use the subs(<dict>) method passing a dictionary that maps each parameter to the desired corresponding value:

new_psi = psi.subs({p: [-1, 2]})
new_psi.to_sympy()
\left[\begin{matrix}\cos{\left(\frac{1}{2} \right)} \cos{\left(\frac{y}{2} \right)} & e^{2.0 i} \sin{\left(\frac{1}{2} \right)} \sin{\left(\frac{y}{2} \right)} & - \sin{\left(\frac{1}{2} \right)} \cos{\left(\frac{y}{2} \right)} & e^{2.0 i} \sin{\left(\frac{y}{2} \right)} \cos{\left(\frac{1}{2} \right)}\end{matrix}\right]

Lambdify a Qiskit circuit

Given a Qiskit circuit, qiskit-symb also allows to generate a Python lambda function with actual arguments matching the Qiskit unbound parameters. Let's consider the following example starting from a ZZFeatureMap circuit, commonly used as a data embedding ansatz in QML applications:

from qiskit.circuit.library import ZZFeatureMap

pqc = ZZFeatureMap(feature_dimension=3, reps=1)
pqc.draw('mpl')

To get the Python function representing the final parametric statevector, we just have to create the symbolic Statevector instance and call the to_lambda() method:

from qiskit_symb.quantum_info import Statevector

pqc = pqc.decompose()
statevec = Statevector(pqc).to_lambda()

We can now call the lambda-generated function statevec passing the x values we want to assign to each parameter. The returned object will be a numpy 2D-array (with shape=(8,1) in this case) representing the final output statevector psi.

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 evaluation and then call the lambda generated function as many times as needed, passing different values of the parameters at each iteration.

Qiskit Medium

Read my blog post introducing to qiskit-symb published on the official Qiskit Medium blog.

Contributors


Simone Gasperini

Sebastian
Brandhofer

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qiskit_symb-0.6.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qiskit_symb-0.6.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file qiskit_symb-0.6.0.tar.gz.

File metadata

  • Download URL: qiskit_symb-0.6.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for qiskit_symb-0.6.0.tar.gz
Algorithm Hash digest
SHA256 d5dc304c5b30dd3d95f0ccaefc43811214f9c13f24685a47b2cd0ce22e09f1fc
MD5 8df3aa8f461e5568d71a9fe2b1c2c3fe
BLAKE2b-256 9635f901ec42c4c9007822327e79ed7344ec80429664524ee90a4694ab60c5b4

See more details on using hashes here.

File details

Details for the file qiskit_symb-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: qiskit_symb-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for qiskit_symb-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35422246af66f64786e7c575776ad81dd63efdeaa9ae3f791835a80b6ca74aa8
MD5 05a4b3fe090f3372b551256e0f64565c
BLAKE2b-256 1cabfa0c905f02eaec8ca37de93b8ae921be4b4a4d180d019acf74e62b8802f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page