Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Generative AI, especially through Generative Adversarial Networks (GANs), revolutionizes content creation across domains by producing highly realistic output. Quantum GANs further elevate this potential by leveraging quantum computing, promising unprecedented advancements in complex data simulation and analysis.
In this notebook, we explore the concept of Quantum Generative Adversarial Networks (QGANs) and implement a simple QGAN model using the Classiq SDK. We study a simple use case of a Bars and Stripes dataset. We begin with a classical implementation of a GAN, and then move to a hybrid quantum-classical GAN model.

1 Data Preparation

We generate the Bars and Stripes dataset, a simple binary dataset consisting of 2x2 images with either a horizontal or vertical stripe pattern:

1.1 Visualizing the Generated Data

Let’s plot a few samples from the dataset to visualize the bars and stripes patterns:
output We create a PyTorch DataLoader to feed the dataset to the GAN model during training:

2 Classical Network

2.1 Defining a Classical GAN

We begin by defining the generator and discriminator models (architecture) for the classical GAN. We work with tensorboard to save our logs (uncomment the following line to install the package):

2.2 Training a Classical GAN

We define the training loop for the classical GAN:
We train our model and save the trained generator in 'generator_model.pth':
Output:

2.3 Evaluating the Performance

Output:
Visualizing generator examples:
output

3 Quantum Hybrid Network Implementation

In this section we define a quantum generator circuit and integrate it into a hybrid quantum-classical GAN model. We then train the QGAN model and evaluate its performance.

3.1 Defining the Quantum GAN

3.1.1 Defining the Quantum Generator

We define the three components of the quantum layer. This is where the quantum network architect’s creativity comes into play! The design we choose:
  1. Data encoding - we take a datum_angle_encoding that encodes nn data points on nn qubits.
  2. A variational ansatz - we combine RY and RZZ gates.
  3. Classical postprocess - we take the vector (p1,p2,,pn)(p_1, p_2, \dots, p_n), with pip_i being the probability to measure 1 on the ii-th qubit.
Finally, we define the quantum model with its hyperparameters as our main quantum function, and synthesize it into a quantum program.
Output:
The resulting circuit: Screenshot 2025-07-02 at 13.53.17.png
Screenshot 2025-07-02 at 13.54.53.png
Screenshot 2025-07-02 at 13.55.05.png

3.1.2 Defining the Hybrid Network

We define the network building blocks: the generator and discriminator in a hybrid network configuration with a quantum layer,

3.3 Training the QGAN

We can use the training loops defined above for the classical GAN:
The following cell generates an archive of the training process in the q_logs directory. We also use Tensorboard to monitor the training in real time. It is possible to use an online version - which is more convenient - but for the purpose of this notebook we use the local version. An example of a vizualization output that can be obtained from tensorboard is shown in the next figure.

Since training can take long time to run, we take a pre-trained model, whose parameters are stored in q_generator_trained_model.pth. In addition, we take a smaller sample size of
  1. (The pre-trained model was trained on 1000 samples.) To train a randomly initialized QGAN, change num_samples from 250 to 1000 for the data creation, and num_epochs from 1 to 10 in the training call train_gan.

Output:

3.3 Evaluating the Performance

Finally, we can evaluate the performance of the QGAN, similar to the classical counterpart:
Output:
Output:

Why do you think the accuracy is so high? Answer: The system chose a metastable pathway where no violation of the rules occurs! Try longer training, different sets of hyperparameters, different architectures, etc.