# Quantum GAN for RGB images

**URL:** <https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923>\
**Category:** PennyLane Help\
**Created:** [May 8, 2023, 4:47pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923 "2023-05-08T16:47:37Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [December 13, 2023, 5:14pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/101 "2023-12-13T17:14:25Z")

</div>

Hi @Eleonora_Panini,

Unfortunately your problem seems just too complicated for a normal laptop. My recommendation would be to work together with someone who has access to HPC resources, like you have worked together with @mass_of_15. This is not a guarantee that you will get a successful result but it can help you avoid memory issues.

On the PennyLane side we keep working on our software to allow you to run larger simulations. You will still reach a limit at some point but I hope we can help you run more complicated problems in the future.

---

<div class="post-metadata">

**Author:** ![Eleonora\_Panini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/eleonora_panini/32/2003_2.png) [@Eleonora\_Panini](https://discuss.pennylane.ai/u/Eleonora_Panini)\
**Post date:** [December 14, 2023, 10:03am UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/102 "2023-12-14T10:03:28Z")

</div>

I can’t have access to an HPC and i don’t know other people who has it. Anyway, when you (and pennylane team) made improvements to the quantum simulator, tell me, so I can try again the training.  
Thank you for your help

---

<div class="post-metadata">

**Author:** ![sana\_falak\_jakati](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sana_falak_jakati/32/2252_2.png) [@sana\_falak\_jakati](https://discuss.pennylane.ai/u/sana_falak_jakati)\
**Post date:** [January 31, 2024, 12:28pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/103 "2024-01-31T12:28:53Z")

</div>

can you guys please share the entire code with me im trying to work on similar problem and want to redo this . Also will convert rgb images with higher resolution inot format that currently demo supports work ?? how does the quantum circuit change in that case

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [January 31, 2024, 11:22pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/104 "2024-01-31T23:22:44Z")

</div>

Hey @sana_falak_jakati, welcome to the forum!

@Eleonora_Panini and @mass_of_15 had some other forum posts / discussions about this (e.g., [How to scale the QGAN example to more qubits](https://discuss.pennylane.ai/t/how-to-scale-the-qgan-example-to-more-qubits/2958)). I think if you search around you might be able to find it. If not, we’ll have to wait for them to respond 😅

---

<div class="post-metadata">

**Author:** ![sana\_falak\_jakati](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sana_falak_jakati/32/2252_2.png) [@sana\_falak\_jakati](https://discuss.pennylane.ai/u/sana_falak_jakati)\
**Post date:** [February 1, 2024, 7:22am UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/105 "2024-02-01T07:22:42Z")

</div>

Hello @isaacdevlugt thanks for the response i was trying out some stuff and images generated arent great so far will post results 🙂 Meanwhile just wanted to check if there are any papers or any leads if someone has tried Pgans using quantum ?

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 2, 2024, 11:28pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/106 "2024-02-02T23:28:25Z")

</div>

I’m not sure that I’ve ever heard of a PGAN 🤔 and when I google it not a whole lot comes up. Can you attach some references for that?

You may have already come across these, but we have some demos on GANs in PennyLane:

> **[PennyLane — Results](https://pennylane.ai/search/?q=GAN)**
>
> A Python library for quantum machine learning, automatic differentiation, and optimization of hybrid quantum-classical computations. Use multiple hardware devices, alongside TensorFlow or PyTorch, in a single computation.

Let me know if that helps!

---

<div class="post-metadata">

**Author:** ![sana\_falak\_jakati](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sana_falak_jakati/32/2252_2.png) [@sana\_falak\_jakati](https://discuss.pennylane.ai/u/sana_falak_jakati)\
**Post date:** [February 9, 2024, 5:46am UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/107 "2024-02-09T05:46:19Z")

</div>

Hello Issacdevlugt,

Here is some details of PGANS " [https://machinelearningmastery.com/introduction-to-progressive-growing-generative-adversarial-networks/](https://machinelearningmastery.com/introduction-to-progressive-growing-generative-adversarial-networks/) " i was looking for progressive GANS which caters to generating high resolution and big scale images .

Is there any similar work done in quantum gans space yet

Thank you for responding 🙂

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 13, 2024, 10:23pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/108 "2024-02-13T22:23:35Z")

</div>

Hey @sana_falak_jakati, to my knowledge, I’m not sure there has been any quantum applied to this. Might be a good thing for you to try out!

---

<div class="post-metadata">

**Author:** ![Eleonora\_Panini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/eleonora_panini/32/2003_2.png) [@Eleonora\_Panini](https://discuss.pennylane.ai/u/Eleonora_Panini)\
**Post date:** [March 5, 2024, 4:02pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/109 "2024-03-05T16:02:34Z")

</div>

HI @sana_falak_jakati did you reach some results with the progressive GAN adapted for quantum circuits and RGB images?

---

<div class="post-metadata">

**Author:** ![mass\_of\_15](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/mass_of_15/32/1430_2.png) [@mass\_of\_15](https://discuss.pennylane.ai/u/mass_of_15)\
**Post date:** [April 5, 2024, 10:10pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/110 "2024-04-05T22:10:27Z")

</div>

Hello @isaacdevlugt, I am want to use `default.qutrit`, instead of `lightning.qubit`. Just a quick question, can I use same Parametrized circuit used in Patch Quantum GAN, using qutrit? Is all the rotation gates and controlled gates, supported in qubit be applied to qutrit? Also I know that from [https://discuss.pennylane.ai/t/how-to-scale-the-qgan-example-to-more-qubits/2958/11?u=mass\_of\_15](https://discuss.pennylane.ai/t/how-to-scale-the-qgan-example-to-more-qubits/2958/11)

> [@How to scale the QGAN example to more qubits](https://discuss.pennylane.ai/t/how-to-scale-the-qgan-example-to-more-qubits/2958/11):
>
> the magic formula you can use: 2^{N - N\_A} = \frac{64}{N\_G}. If that equation holds, then those values of N\_G, N, N\_A will work.

So If I use qutrit, will each wires give three probability values. What would be the equation then?

Last question, will the the runtime speedup?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [April 8, 2024, 2:57pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/111 "2024-04-08T14:57:22Z")

</div>

Hi @mass_of_15 ! Thank you for your question.

Since this thread is already very long and your question is related more to qutrits than to GANs, would you be ok with opening a new thread with your question? This way we can ask our qutrits expert and it will be easier for them to help you. If you could post the link to the new thread here that would be great.

And just in case you haven’t seen it, we have a [demo on qutrits](https://pennylane.ai/qml/demos/tutorial_qutrits_bernstein_vazirani/) which might be interesting for you!

---

<div class="post-metadata">

**Author:** ![mass\_of\_15](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/mass_of_15/32/1430_2.png) [@mass\_of\_15](https://discuss.pennylane.ai/u/mass_of_15)\
**Post date:** [April 8, 2024, 3:48pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/112 "2024-04-08T15:48:42Z")

</div>

> [@Qutrits for quantum GANs](https://discuss.pennylane.ai/t/qutrits-for-quantum-gans/4409):
>
> I am want to use default.qutrit , instead of lightning.qubit . Just a quick question, can I use same Parametrized circuit used in Patch Quantum GAN, using qutrit? Is all the rotation gates and controlled gates, supported in qubit be applied to qutrit? Are there any list of available gates to be used in qutrits. In the demo for qutrits, I saw the gates such as THadamard, TShift and TAdd gates. What all other gates are there? Any parameterized gates, such as rotation gates. Also I know that from [h…](https://discuss.pennylane.ai/t/how-to-scale-the-qgan-example-to-more-qubits/2958/11)

I have created new thread for qutrits as requested.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [April 9, 2024, 8:39pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/113 "2024-04-09T20:39:41Z")

</div>

Thank you @mass_of_15 ! I’ll continue the conversation there.

---

<div class="post-metadata">

**Author:** ![thewill-i-am](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/thewill-i-am/32/3298_2.png) [@thewill-i-am](https://discuss.pennylane.ai/u/thewill-i-am)\
**Post date:** [October 30, 2024, 8:05pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/114 "2024-10-30T20:05:58Z")

</div>

@mass_of_15  
Hello, I am trying to do this same example on an HPC but with a different dataset. Could you send me your code, I would like to see it, or maybe you have it in a github repo?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [October 31, 2024, 7:14pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/115 "2024-10-31T19:14:24Z")

</div>

Hi @thewill-i-am , welcome to the Forum!

Have you tried out our demos on QGANs?

> **[PennyLane — Results](https://pennylane.ai/search/?q=GAN)**
>
> A Python library for quantum machine learning, automatic differentiation, and optimization of hybrid quantum-classical computations. Use multiple hardware devices, alongside TensorFlow or PyTorch, in a single computation.

---

<div class="post-metadata">

**Author:** ![thewill-i-am](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/thewill-i-am/32/3298_2.png) [@thewill-i-am](https://discuss.pennylane.ai/u/thewill-i-am)\
**Post date:** [October 31, 2024, 7:33pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/117 "2024-10-31T19:33:56Z")

</div>

@CatalinaAlbornoz

@CatalinaAlbornoz , Hello greetings from Costa Rica

Yes, of course I already saw that result, I programmed the version for colors, in fact I already have it ready, I am running it Google COLAB with few iterations and using a GPU

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/4/4f781ca522185dea42e76ffeb125eaa591a6a212.jpeg)

```auto
# Define the quantum circuit and auxiliary functions
@qml.qnode(dev, interface='torch', diff_method='parameter-shift')
def quantum_circuit(noise, weights):
    weights = weights.reshape(q_depth, n_qubits)

    # Initialize the qubits with the latent vector
    for i in range(n_qubits):
        qml.RY(noise[i], wires=i)

    # Apply parameterized layers
    for i in range(q_depth):
        for y in range(n_qubits):
            qml.RY(weights[i][y], wires=y)
        # CZ Gates
        for y in range(n_qubits - 1):
            qml.CZ(wires=[y, y + 1])

    return qml.probs(wires=list(range(n_qubits)))

def partial_measure(x, weights):
    """
    Applies the quantum circuit to each element in the batch.

    Args:
        x (torch.Tensor): Tensor of shape (batch_size, n_qubits)
        weights (torch.Tensor): Tensor of parameters of shape (q_depth * n_qubits)

    Returns:
        torch.Tensor: Post-processed probabilities of shape (batch_size, 2**(n_qubits - n_a_qubits))
    """
    batch_size = x.size(0)
    probs_list = []

    for i in range(batch_size):
        noise = x[i].float().to(device) # Ensure noise is float32 and on the correct device
        weights_tensor = weights.float().to(device) # Ensure weights is float32 and on the correct device
        probs = quantum_circuit(noise, weights_tensor) # Shape: (2**n_qubits,)
        probsgiven0 = probs[: (2 ** (n_qubits - n_a_qubits))]
        probsgiven0 /= torch.sum(probs)
        probsgiven = probsgiven0 / torch.max(probsgiven0)
        probs_list.append(probsgiven)

    probs_tensor = torch.stack(probs_list, dim=0) # (batch_size, 2**(n_qubits - n_a_qubits))
    return probs_tensor

# Define the quantum generator class
class PatchQuantumGenerator(nn.Module):
    """Quantum generator for the patch method, adapted for 16x16 RGB images"""

    def __init__ (self, n_generators, q_delta=1):
        """
        Args:
            n_generators (int): Number of sub-generators per channel (should be 1 for 16x16 RGB images).
            q_delta (float, optional): Range of the random distribution for parameter initialization.
        """
        super(). __init__ ()
        # Generators for each color channel
        self.red_params = nn.ParameterList([
            nn.Parameter(q_delta * torch.rand(q_depth * n_qubits, device=device, dtype=torch.float32), requires_grad=True)
            for _ in range(n_generators)
        ])
        self.green_params = nn.ParameterList([
            nn.Parameter(q_delta * torch.rand(q_depth * n_qubits, device=device, dtype=torch.float32), requires_grad=True)
            for _ in range(n_generators)
        ])
        self.blue_params = nn.ParameterList([
            nn.Parameter(q_delta * torch.rand(q_depth * n_qubits, device=device, dtype=torch.float32), requires_grad=True)
            for _ in range(n_generators)
        ])
        self.n_generators = n_generators

    def forward(self, x):
        """
        Args:
            x (torch.Tensor): Noise tensor of shape (batch_size, n_qubits)
        Returns:
            torch.Tensor: Generated images of shape (batch_size, 3, image_size, image_size)
        """
        x = x.float() # Ensure x is float32
        patch_size = 2 ** (n_qubits - n_a_qubits) # 256
        batch_size = x.size(0)

        # Lists for each channel
        red_patches = []
        green_patches = []
        blue_patches = []

        # Generate patches for each channel
        for params in self.red_params:
            patches = partial_measure(x, params) # Shape: (batch_size, patch_size)
            red_patches.append(patches)
        red_patches = torch.cat(red_patches, dim=1) # Shape: (batch_size, n_generators * patch_size)

        for params in self.green_params:
            patches = partial_measure(x, params)
            green_patches.append(patches)
        green_patches = torch.cat(green_patches, dim=1)

        for params in self.blue_params:
            patches = partial_measure(x, params)
            blue_patches.append(patches)
        blue_patches = torch.cat(blue_patches, dim=1)

        # Concatenate the three channels
        images = torch.cat([red_patches, green_patches, blue_patches], dim=1) # (batch_size, 3 * n_generators * patch_size)

        # Reshape to (batch_size, 3, image_size, image_size)
        images = images.view(batch_size, 3, image_size, image_size)
        images = images.float() # Ensure images are float32
        return images

# Define the classical Discriminator class
class Discriminator(nn.Module):
    def __init__ (self):
        super(Discriminator, self). __init__ ()
        self.model = nn.Sequential(
            nn.Linear(image_size * image_size * 3, 2048), # Reduced from 4096 to 2048
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(2048, 512), # Reduced from 1024 to 512
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(512, 128), # Reduced from 256 to 128
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(128, 1),
            nn.Sigmoid()
        )

    def forward(self, x):
        x = x.view(x.size(0), -1) # Flatten to (batch_size, 3*image_size*image_size)
        x = x.float() # Ensure x is float32
        return self.model(x)

```

This is the configuration I am using for the qubits, etc:

```auto
n_qubits = 9  
n_a_qubits = 1  
q_depth = 2  
image_size = 16  
batch_size = 4  
n_generators = 1 
nz = n_qubits # nz = 9
epochs = 10

```

---

<div class="post-metadata">

**Author:** ![thewill-i-am](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/thewill-i-am/32/3298_2.png) [@thewill-i-am](https://discuss.pennylane.ai/u/thewill-i-am)\
**Post date:** [October 31, 2024, 7:35pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/118 "2024-10-31T19:35:47Z")

</div>

@CatalinaAlbornoz I’m getting bad results, well like the one I sent above, I imagine it must be because I’m using few epocs, but I’m not sure

---

<div class="post-metadata">

**Author:** ![thewill-i-am](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/thewill-i-am/32/3298_2.png) [@thewill-i-am](https://discuss.pennylane.ai/u/thewill-i-am)\
**Post date:** [October 31, 2024, 9:57pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/119 "2024-10-31T21:57:25Z")

</div>

This is with 50 Epochs, I think is going to improve a little bit while the Epochs increase

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/7/75ae182070e05d6232c12381117dafb712c4a173.jpeg)

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [October 31, 2024, 11:52pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/120 "2024-10-31T23:52:47Z")

</div>

Hi @thewill-i-am, nice progression!

These problems usually require LOTS of resources on real datasets. You may need more qubits than what you can actually run on Google Colab. One technique that can work is reducing the dimensionality of your dataset. You basically downgrade your data but this might allow you to have enough computational resources to simulate it. This pre-processing of the data is usually done classically.

Nevertheless, some problems are just not well suited for QGANs so there’s no actual guarantee that it will work for your specific dataset.

I hope this helps.

---

<div class="post-metadata">

**Author:** ![qimin](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/qimin/32/3454_2.png) [@qimin](https://discuss.pennylane.ai/u/qimin)\
**Post date:** [December 20, 2024, 3:41pm UTC](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923/121 "2024-12-20T15:41:00Z")

</div>

Hello, I want to ask a question. If I want to encode RGB images into quantum states as input to the quantum generator, how can I do it?  
Another question, I am trying to use quantum generative adversarial networks to generate hyperspectral images. I saw this post. It is very difficult to generate RGB images at present. It seems to be even more difficult to generate hyperspectral images.Do you have any good suggestions? Thank you.

[Previous page](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923.md?page=5)

[Next page](https://discuss.pennylane.ai/t/quantum-gan-for-rgb-images/2923.md?page=7)
