# Quantum Generative Adversarial Network (qGAN)

Loading an arbitrary random probability distribution into an n-qubit quantum state normally requires $O(2^n)$ gates which in most algorithms will dominate the complexity of the quantum algorithm and make it useless. By using a qGAN this loading can be done in $O(poly(n))$ gates [[1](https://https://www.nature.com/articles/s41534-019-0223-2)].

A qGAN is a quantum version of a [Generative Adversarial Network](https://papers.nips.cc/paper/5423-generative-adversarial-nets) with a quantum generator and a classical discriminator. The quantum generator is trained to transform a given n-qubit input into:
$$
\sum_{j=0}^{2^n-1} \sqrt{p^j_{\theta}}\left| j \right\rangle
$$
where $p^j_{\theta}$ is the probabilty of the state $j$. The discriminator has to try and distinguish between the output of the generator and the training data set. The two networks train alternatively and will eventaully reach a nash equilibrium where the discriminator cannot tell apart the generator and the training set data. The aim of this process is for $p^j_{\theta}$ to approximate the distribution of the training data.

This tutorial will guide you through using a qGAN to load a lognormal distribution to a 2 qubit system.

# Setup

```
!pip install --upgrade tensorflow==2.1.0  tensorflow-quantum tensorflow-gan tensorflow-probability==0.9 tensorflow-datasets
```

    Collecting tensorflow==2.1.0
      Downloading https://files.pythonhosted.org/packages/85/d4/c0cd1057b331bc38b65478302114194bd8e1b9c2bbc06e300935c0e93d90/tensorflow-2.1.0-cp36-cp36m-manylinux2010_x86_64.whl (421.8MB)
         |████████████████████████████████| 421.8MB 32kB/s
    Collecting tensorflow-quantum
      Downloading https://files.pythonhosted.org/packages/eb/76/766f7ef3f57abf1467bf1a591a5e6776f1166420a90ea05af9ca5e25d768/tensorflow_quantum-0.3.1-cp36-cp36m-manylinux2010_x86_64.whl (3.9MB)
         |████████████████████████████████| 3.9MB 46.6MB/s
    Collecting tensorflow-gan
      Downloading https://files.pythonhosted.org/packages/0c/2e/62922111d7d50e1900e3030764743ea7735540ce103b3ab30fd5cd2d8a2b/tensorflow_gan-2.0.0-py2.py3-none-any.whl (365kB)
         |████████████████████████████████| 368kB 41.4MB/s
    Collecting tensorflow-probability==0.9
      Downloading https://files.pythonhosted.org/packages/9b/ed/f587d64127bbb85e539f06a2aace1240b7b5c6b4267bea94f232230551a5/tensorflow_probability-0.9.0-py2.py3-none-any.whl (3.2MB)
         |████████████████████████████████| 3.2MB 40.3MB/s
    Collecting tensorflow-datasets
      Downloading https://files.pythonhosted.org/packages/ca/c9/d97bdf931edbae9aebc767633d088bd674136d5fe7587ef693b7cb6a1883/tensorflow_datasets-3.2.1-py3-none-any.whl (3.4MB)
         |████████████████████████████████| 3.4MB 48.3MB/s
    Requirement already satisfied, skipping upgrade: six>=1.12.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.15.0)
    Collecting keras-applications>=1.0.8
      Downloading https://files.pythonhosted.org/packages/71/e3/19762fdfc62877ae9102edf6342d71b28fbfd9dea3d2f96a882ce099b03f/Keras_Applications-1.0.8-py3-none-any.whl (50kB)
         |████████████████████████████████| 51kB 7.2MB/s
    Requirement already satisfied, skipping upgrade: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (0.8.1)
    Requirement already satisfied, skipping upgrade: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (0.9.0)
    Requirement already satisfied, skipping upgrade: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (0.34.2)
    Requirement already satisfied, skipping upgrade: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.12.1)
    Requirement already satisfied, skipping upgrade: keras-preprocessing>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.1.2)
    Requirement already satisfied, skipping upgrade: protobuf>=3.8.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (3.12.4)
    Requirement already satisfied, skipping upgrade: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.1.0)
    Collecting tensorflow-estimator<2.2.0,>=2.1.0rc0
      Downloading https://files.pythonhosted.org/packages/18/90/b77c328a1304437ab1310b463e533fa7689f4bfc41549593056d812fab8e/tensorflow_estimator-2.1.0-py2.py3-none-any.whl (448kB)
         |████████████████████████████████| 450kB 37.6MB/s
    Requirement already satisfied, skipping upgrade: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (3.3.0)
    Requirement already satisfied, skipping upgrade: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.31.0)
    Collecting tensorboard<2.2.0,>=2.1.0
      Downloading https://files.pythonhosted.org/packages/d9/41/bbf49b61370e4f4d245d4c6051dfb6db80cec672605c91b1652ac8cc3d38/tensorboard-2.1.1-py3-none-any.whl (3.8MB)
         |████████████████████████████████| 3.9MB 44.6MB/s
    Requirement already satisfied, skipping upgrade: scipy==1.4.1; python_version >= "3" in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.4.1)
    Requirement already satisfied, skipping upgrade: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (1.18.5)
    Collecting gast==0.2.2
      Downloading https://files.pythonhosted.org/packages/4e/35/11749bf99b2d4e3cceb4d55ca22590b0d7c2c62b9de38ac4a4a7f4687421/gast-0.2.2.tar.gz
    Requirement already satisfied, skipping upgrade: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.1.0) (0.2.0)
    Collecting sympy==1.4
      Downloading https://files.pythonhosted.org/packages/21/21/f4105795ca7f35c541d82c5b06be684dd2f5cb4f508fb487cd7aea4de776/sympy-1.4-py2.py3-none-any.whl (5.3MB)
         |████████████████████████████████| 5.3MB 37.9MB/s
    Collecting pathos==0.2.5
      Downloading https://files.pythonhosted.org/packages/c6/a2/cd59f73d5ede4f122687bf8f63de5d671c9561e493ca699241f05b038278/pathos-0.2.5.tar.gz (162kB)
         |████████████████████████████████| 163kB 50.9MB/s
    Collecting cirq==0.8.0
      Downloading https://files.pythonhosted.org/packages/b8/cf/d95635b4b0de2227956077687277426299883ccdb9e6418fa6e599893ea3/cirq-0.8.0-py3-none-any.whl (1.4MB)
         |████████████████████████████████| 1.4MB 44.8MB/s
    Requirement already satisfied, skipping upgrade: tensorflow-hub>=0.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gan) (0.8.0)
    Requirement already satisfied, skipping upgrade: decorator in /usr/local/lib/python3.6/dist-packages (from tensorflow-probability==0.9) (4.4.2)
    Requirement already satisfied, skipping upgrade: cloudpickle>=1.2.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow-probability==0.9) (1.3.0)
    Requirement already satisfied, skipping upgrade: tensorflow-metadata in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (0.22.2)
    Requirement already satisfied, skipping upgrade: requests>=2.19.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (2.23.0)
    Requirement already satisfied, skipping upgrade: dill in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (0.3.2)
    Requirement already satisfied, skipping upgrade: promise in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (2.3)
    Requirement already satisfied, skipping upgrade: attrs>=18.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (19.3.0)
    Requirement already satisfied, skipping upgrade: tqdm in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (4.41.1)
    Requirement already satisfied, skipping upgrade: future in /usr/local/lib/python3.6/dist-packages (from tensorflow-datasets) (0.16.0)
    Requirement already satisfied, skipping upgrade: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.8->tensorflow==2.1.0) (2.10.0)
    Requirement already satisfied, skipping upgrade: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.8.0->tensorflow==2.1.0) (49.2.0)
    Requirement already satisfied, skipping upgrade: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (3.2.2)
    Requirement already satisfied, skipping upgrade: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (1.0.1)
    Requirement already satisfied, skipping upgrade: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (1.17.2)
    Requirement already satisfied, skipping upgrade: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (0.4.1)
    Requirement already satisfied, skipping upgrade: mpmath>=0.19 in /usr/local/lib/python3.6/dist-packages (from sympy==1.4->tensorflow-quantum) (1.1.0)
    Collecting ppft>=1.6.6.1
      Downloading https://files.pythonhosted.org/packages/2b/fb/fa21f6e9aedc4823448473ed96e8eab64af1cb248c18165f045a90e1c6b4/ppft-1.6.6.2.zip (106kB)
         |████████████████████████████████| 112kB 48.6MB/s
    Collecting pox>=0.2.7
      Downloading https://files.pythonhosted.org/packages/1a/0c/ec447fb0ed88bc1c09bf0dadf00e40ea05fda17e841d15bb351a52d9e192/pox-0.2.8.zip (128kB)
         |████████████████████████████████| 133kB 48.6MB/s
    Requirement already satisfied, skipping upgrade: multiprocess>=0.70.9 in /usr/local/lib/python3.6/dist-packages (from pathos==0.2.5->tensorflow-quantum) (0.70.10)
    Requirement already satisfied, skipping upgrade: pandas in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (1.0.5)
    Requirement already satisfied, skipping upgrade: dataclasses; python_version < "3.7" in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (0.7)
    Requirement already satisfied, skipping upgrade: networkx~=2.4 in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (2.4)
    Requirement already satisfied, skipping upgrade: matplotlib~=3.0 in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (3.2.2)
    Requirement already satisfied, skipping upgrade: sortedcontainers~=2.0 in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (2.2.2)
    Requirement already satisfied, skipping upgrade: typing-extensions in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (3.7.4.2)
    Collecting freezegun~=0.3.15
      Downloading https://files.pythonhosted.org/packages/17/5d/1b9d6d3c7995fff473f35861d674e0113a5f0bd5a72fe0199c3f254665c7/freezegun-0.3.15-py2.py3-none-any.whl
    Requirement already satisfied, skipping upgrade: google-api-core[grpc]<2.0.0dev,>=1.14.0 in /usr/local/lib/python3.6/dist-packages (from cirq==0.8.0->tensorflow-quantum) (1.16.0)
    Requirement already satisfied, skipping upgrade: googleapis-common-protos in /usr/local/lib/python3.6/dist-packages (from tensorflow-metadata->tensorflow-datasets) (1.52.0)
    Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests>=2.19.0->tensorflow-datasets) (1.24.3)
    Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.19.0->tensorflow-datasets) (2020.6.20)
    Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.19.0->tensorflow-datasets) (2.10)
    Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests>=2.19.0->tensorflow-datasets) (3.0.4)
    Requirement already satisfied, skipping upgrade: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.6/dist-packages (from markdown>=2.6.8->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (1.7.0)
    Requirement already satisfied, skipping upgrade: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (0.2.8)
    Requirement already satisfied, skipping upgrade: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (4.1.1)
    Requirement already satisfied, skipping upgrade: rsa<5,>=3.1.4; python_version >= "3" in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (4.6)
    Requirement already satisfied, skipping upgrade: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (1.3.0)
    Requirement already satisfied, skipping upgrade: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas->cirq==0.8.0->tensorflow-quantum) (2.8.1)
    Requirement already satisfied, skipping upgrade: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->cirq==0.8.0->tensorflow-quantum) (2018.9)
    Requirement already satisfied, skipping upgrade: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib~=3.0->cirq==0.8.0->tensorflow-quantum) (2.4.7)
    Requirement already satisfied, skipping upgrade: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib~=3.0->cirq==0.8.0->tensorflow-quantum) (1.2.0)
    Requirement already satisfied, skipping upgrade: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib~=3.0->cirq==0.8.0->tensorflow-quantum) (0.10.0)
    Requirement already satisfied, skipping upgrade: zipp>=0.5 in /usr/local/lib/python3.6/dist-packages (from importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (3.1.0)
    Requirement already satisfied, skipping upgrade: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.6/dist-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (0.4.8)
    Requirement already satisfied, skipping upgrade: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.2.0,>=2.1.0->tensorflow==2.1.0) (3.1.0)
    Building wheels for collected packages: gast, pathos, ppft, pox
      Building wheel for gast (setup.py) ... done
      Created wheel for gast: filename=gast-0.2.2-cp36-none-any.whl size=7540 sha256=2ad6cbdc07e51d75f24df6a3c5431e851f78bdf88af71c690910e7e848ef4f5e
      Stored in directory: /root/.cache/pip/wheels/5c/2e/7e/a1d4d4fcebe6c381f378ce7743a3ced3699feb89bcfbdadadd
      Building wheel for pathos (setup.py) ... done
      Created wheel for pathos: filename=pathos-0.2.5-cp36-none-any.whl size=77577 sha256=343c596b255ac0aacd0809ba6d675783772d715ee8822532e84af24239f89ebb
      Stored in directory: /root/.cache/pip/wheels/61/6d/83/90b0c3d2c271da2c4850731e894798c98f8dbedbac74e8eff0
      Building wheel for ppft (setup.py) ... done
      Created wheel for ppft: filename=ppft-1.6.6.2-cp36-none-any.whl size=64743 sha256=8f1da4f2e681b1565a8a86757300af1eb50de1cf1530f7308bb208d3f2827963
      Stored in directory: /root/.cache/pip/wheels/db/d2/2d/0ee21ede61786bb13247dbc69079373fd500c2bb0481913084
      Building wheel for pox (setup.py) ... done
      Created wheel for pox: filename=pox-0.2.8-cp36-none-any.whl size=28290 sha256=bf2b329588e208ed398a506e3f3f94fb45284cf1a70df4d8a0dd42e429525219
      Stored in directory: /root/.cache/pip/wheels/39/ed/ce/a93103746b327e18bffaeb99ba0d57a88b392f31d719cea700
    Successfully built gast pathos ppft pox
    ERROR: cirq 0.8.0 has requirement protobuf==3.8.0, but you'll have protobuf 3.12.4 which is incompatible.
    Installing collected packages: keras-applications, tensorflow-estimator, tensorboard, gast, tensorflow, sympy, ppft, pox, pathos, freezegun, cirq, tensorflow-quantum, tensorflow-probability, tensorflow-gan, tensorflow-datasets
      Found existing installation: tensorflow-estimator 2.3.0
        Uninstalling tensorflow-estimator-2.3.0:
          Successfully uninstalled tensorflow-estimator-2.3.0
      Found existing installation: tensorboard 2.3.0
        Uninstalling tensorboard-2.3.0:
          Successfully uninstalled tensorboard-2.3.0
      Found existing installation: gast 0.3.3
        Uninstalling gast-0.3.3:
          Successfully uninstalled gast-0.3.3
      Found existing installation: tensorflow 2.3.0
        Uninstalling tensorflow-2.3.0:
          Successfully uninstalled tensorflow-2.3.0
      Found existing installation: sympy 1.1.1
        Uninstalling sympy-1.1.1:
          Successfully uninstalled sympy-1.1.1
      Found existing installation: tensorflow-probability 0.11.0
        Uninstalling tensorflow-probability-0.11.0:
          Successfully uninstalled tensorflow-probability-0.11.0
      Found existing installation: tensorflow-datasets 2.1.0
        Uninstalling tensorflow-datasets-2.1.0:
          Successfully uninstalled tensorflow-datasets-2.1.0
    Successfully installed cirq-0.8.0 freezegun-0.3.15 gast-0.2.2 keras-applications-1.0.8 pathos-0.2.5 pox-0.2.8 ppft-1.6.6.2 sympy-1.4 tensorboard-2.1.1 tensorflow-2.1.0 tensorflow-datasets-3.2.1 tensorflow-estimator-2.1.0 tensorflow-gan-2.0.0 tensorflow-probability-0.9.0 tensorflow-quantum-0.3.1

```
import tensorflow as tf
import tensorflow_quantum as tfq
import tensorflow_gan as tfg

import cirq
import sympy
import numpy as np
import collections
import math

import matplotlib.pyplot as plt

# Initialize qubits
num_qubits =  2#@param
qubits = [cirq.GridQubit(x,0) for x in range(num_qubits)]
num_of_samples = 100 # Size of training data set
sample_size = 50
tf.keras.backend.set_floatx('float32')

```

# Load Training Data

Before building the model, you need to generate the training data set.

```
def generate_data():
  """Generate training data for discriminator

     Bundles this with noise for generator to use
  """

  # Take samples of lognormal distribution with mean = 1
  # and standard deviation =1
  mu =1
  sigma =1

  total = []

  continuous_data = np.random.lognormal(
      mean=mu, sigma=sigma, size=sample_size*num_of_samples)

  # Remove all samples that lie outside the range
  # expressible in the given number of qubits
  continuous_data = continuous_data[continuous_data < 2**num_qubits-0.5]

  # Crop the data so it is a multiple of the sample size
  # This can cause problems if the sample size is very low (e.g. 1) as it can
  # crop all the way to empty
  continuous_data = continuous_data[:len(continuous_data)
      //sample_size* sample_size]

  # Discretize the remaining data so the continuous distribution can be
  # approximated by a discrete distribution
  discrete_data = tf.convert_to_tensor(
      np.around(continuous_data, decimals=0), dtype=tf.dtypes.float32)

  # Split the data into batches of the required sample size
  batches = tf.reshape(
      discrete_data,(len(continuous_data)//sample_size,sample_size))

  # Initialize the same number of circuits as the discrete tensor to a uniform
  # distribution by applying multiple Hadamard gates
  noise = tfq.convert_to_tensor(
      [cirq.Circuit(
          cirq.H.on_each(qubits)
          ) for _ in range(len(continuous_data))])

  return noise, batches

```

# Quantum Generator

Each layer of a quantum generator consists of a layer of parameterised $R_y$ rotations, and a layer of $CZ$ gates to entangle all the qubits.

The quantum generator you will be using only is only one layer deep. To represent more complex structures a larger circuit depth would need to be used.

```
def quantum_generator_model(initial_distribution_tensor):
  # Create parameters for each qubit
  theta = sympy.symbols(f'a0:{num_qubits}')

  # Set the input to the network
  inputs = tf.keras.Input(shape=(), dtype=tf.dtypes.string)

  # Create the parameterised Ry rotation layer circuit
  parameterized_circuit = cirq.Circuit(cirq.Moment(
      [cirq.ry(t)(q) for t, q in zip(theta,qubits)]))

  # Entangle all the qubits by applying CZ in a circular fashion
  # except when there are only two qubits and then just apply one CZ
  entangle_circuit = cirq.Circuit(
    [cirq.CZ(q1, q2) for q1, q2 in zip(qubits[0:-1], qubits[1:])])
  if(num_qubits > 2):
    entangle_circuit.append([cirq.CZ(qubits[0], qubits[-1])])

  # Combine the parameterized circuit layer and the entanglement circuit layer
  layer_circuit =  parameterized_circuit + entangle_circuit
  print(layer_circuit)

  # Add this circuit layer to the network with the output configured
  # to return a decimal value equivalent to the binary produced from the Z
  # measurement with -1 mapping to 0, 1 mapping to 1
  observable = sum((cirq.Z(qubits[i])+1)/2*2**i for i in range(num_qubits))
  # Repetitions is set to 1 so integers are output, rather than averages
  layer = tfq.layers.PQC(layer_circuit, observable , repetitions=1)(inputs)

  model = tf.keras.Model(inputs=[inputs], outputs=[layer])
  #model.summary()
  return tf.cast(tf.reshape(
      model(initial_distribution_tensor),
      (initial_distribution_tensor.shape[0] // sample_size,
       sample_size)),dtype=tf.float32)

# Test the quantum generator
noise, real_data = generate_data()
data = quantum_generator_model(noise)
print(data)
print(real_data)
```

    (0, 0): ───Ry(a0)───@───
                        │
    (1, 0): ───Ry(a1)───@───
    tf.Tensor(
    [[0. 2. 1. ... 0. 2. 1.]
     [0. 0. 1. ... 1. 1. 1.]
     [0. 1. 1. ... 0. 1. 1.]
     ...
     [1. 1. 1. ... 1. 1. 1.]
     [1. 1. 3. ... 2. 1. 1.]
     [3. 1. 1. ... 1. 0. 2.]], shape=(598, 50), dtype=float32)
    tf.Tensor(
    [[2. 3. 0. ... 2. 2. 3.]
     [3. 3. 1. ... 1. 3. 1.]
     [1. 2. 0. ... 3. 3. 3.]
     ...
     [1. 3. 2. ... 3. 3. 1.]
     [1. 3. 3. ... 1. 1. 3.]
     [3. 1. 1. ... 2. 1. 2.]], shape=(598, 50), dtype=float32)

# Discriminator

The discriminator is a classical neural network. You will use a 3-layer network with an input layer, a hidden layer with 50 hidden nodes, a hidden layer with 20 hidden nodes and 1 output node. The structure of the discriminator is picked so it is equally balanced with the generator by emperical methods (we have just used the same structure as https://www.nature.com/articles/s41534-019-0223-2).

```
def discriminator_model(real_input, gen_inputs):

  model = tf.keras.Sequential()
  model.add(tf.keras.Input(shape=(sample_size,)))
  model.add(tf.keras.layers.Dense(20, activation="relu"))
  model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
  #model.summary()
  print(real_input)

  return model(real_input)

d1 = discriminator_model(data, noise)
print(d1)
d2 = discriminator_model(real_data, noise)
print(d2)
```

    tf.Tensor(
    [[0. 2. 1. ... 0. 2. 1.]
     [0. 0. 1. ... 1. 1. 1.]
     [0. 1. 1. ... 0. 1. 1.]
     ...
     [1. 1. 1. ... 1. 1. 1.]
     [1. 1. 3. ... 2. 1. 1.]
     [3. 1. 1. ... 1. 0. 2.]], shape=(598, 50), dtype=float32)
    tf.Tensor(
    [[0.04196338]
     [0.27266592]
     [0.20387009]
     [0.10551755]
     [0.33895066]
     [0.5607761 ]
     [0.01904764]
     [0.5698652 ]
     [0.06592283]
     [0.17044581]
     [0.10697818]
     [0.29591376]
     [0.11277947]
     [0.14306422]
     [0.278469  ]
     [0.17531624]
     [0.39308968]
     [0.09315131]
     [0.29625157]
     [0.15907799]
     [0.2871086 ]
     [0.12121072]
     [0.3500702 ]
     [0.3817374 ]
     [0.20325089]
     [0.3309933 ]
     [0.01982394]
     [0.13464607]
     [0.20136324]
     [0.21777107]
     [0.11375783]
     [0.09663168]
     [0.16167657]
     [0.3930824 ]
     [0.23511837]
     [0.17967328]
     [0.15124165]
     [0.14190216]
     [0.3723028 ]
     [0.21404546]
     [0.24877833]
     [0.16717948]
     [0.1358577 ]
     [0.20491494]
     [0.22274303]
     [0.07413235]
     [0.11708534]
     [0.30987257]
     [0.12337372]
     [0.20173752]
     [0.06520469]
     [0.5435832 ]
     [0.10337945]
     [0.23658587]
     [0.16840404]
     [0.5805596 ]
     [0.36362392]
     [0.11897162]
     [0.07900479]
     [0.19759305]
     [0.09132273]
     [0.2922706 ]
     [0.16292569]
     [0.20711577]
     [0.15527584]
     [0.24857406]
     [0.42944127]
     [0.08555594]
     [0.31653142]
     [0.11382154]
     [0.27301946]
     [0.3312118 ]
     [0.12812293]
     [0.09796012]
     [0.14760566]
     [0.18759163]
     [0.5132539 ]
     [0.25072643]
     [0.22716676]
     [0.09929606]
     [0.17957759]
     [0.21764185]
     [0.21378581]
     [0.2936371 ]
     [0.33228248]
     [0.3479959 ]
     [0.28782567]
     [0.53356403]
     [0.13577795]
     [0.18961726]
     [0.24788024]
     [0.3533282 ]
     [0.2546042 ]
     [0.20243661]
     [0.2862381 ]
     [0.08501486]
     [0.07385254]
     [0.21679498]
     [0.16410054]
     [0.32516328]
     [0.05822   ]
     [0.11608237]
     [0.3578469 ]
     [0.22392389]
     [0.34429163]
     [0.10261083]
     [0.2088847 ]
     [0.30106467]
     [0.45299026]
     [0.49620607]
     [0.20326409]
     [0.06778032]
     [0.7153322 ]
     [0.30489525]
     [0.16668092]
     [0.22238383]
     [0.0691215 ]
     [0.01659771]
     [0.30600655]
     [0.36269993]
     [0.29198226]
     [0.41954267]
     [0.26866135]
     [0.21288632]
     [0.14493528]
     [0.14216948]
     [0.29651895]
     [0.27291197]
     [0.20239069]
     [0.35704246]
     [0.5428234 ]
     [0.2600717 ]
     [0.21347351]
     [0.2968389 ]
     [0.09215328]
     [0.35460672]
     [0.23257974]
     [0.26780102]
     [0.04146734]
     [0.2559512 ]
     [0.6137853 ]
     [0.09937537]
     [0.35039848]
     [0.13428591]
     [0.5188672 ]
     [0.4413665 ]
     [0.5276289 ]
     [0.26431334]
     [0.36197078]
     [0.15760385]
     [0.27921703]
     [0.05298775]
     [0.4846103 ]
     [0.05334086]
     [0.18913414]
     [0.46737018]
     [0.15556085]
     [0.28005055]
     [0.08902469]
     [0.07966176]
     [0.12418076]
     [0.46383002]
     [0.17478414]
     [0.25536498]
     [0.09822258]
     [0.10366845]
     [0.23505408]
     [0.5360304 ]
     [0.17596556]
     [0.22558813]
     [0.07362123]
     [0.02400539]
     [0.66499877]
     [0.22945385]
     [0.10318703]
     [0.0573759 ]
     [0.15454674]
     [0.34921587]
     [0.1442583 ]
     [0.22983047]
     [0.2714374 ]
     [0.30758095]
     [0.18718617]
     [0.19894713]
     [0.20480075]
     [0.09426221]
     [0.2495385 ]
     [0.19955698]
     [0.23072147]
     [0.20177914]
     [0.28022075]
     [0.14812301]
     [0.4967185 ]
     [0.0834794 ]
     [0.28931916]
     [0.38114804]
     [0.5260778 ]
     [0.30080977]
     [0.333921  ]
     [0.5146654 ]
     [0.39540526]
     [0.35677415]
     [0.05867711]
     [0.14411446]
     [0.22264235]
     [0.2521497 ]
     [0.18305856]
     [0.05115403]
     [0.05842955]
     [0.3428932 ]
     [0.20221955]
     [0.12698591]
     [0.09053931]
     [0.43108705]
     [0.05923652]
     [0.1732673 ]
     [0.23359935]
     [0.20609319]
     [0.12625048]
     [0.15840346]
     [0.17453499]
     [0.29175007]
     [0.26664305]
     [0.18714043]
     [0.2999413 ]
     [0.2264876 ]
     [0.15357219]
     [0.17315981]
     [0.15065736]
     [0.23067361]
     [0.2469291 ]
     [0.21044287]
     [0.21314237]
     [0.20511949]
     [0.3538142 ]
     [0.49692377]
     [0.23420133]
     [0.17143868]
     [0.14640422]
     [0.20905313]
     [0.20226274]
     [0.09016147]
     [0.33379847]
     [0.16767494]
     [0.20347783]
     [0.5059603 ]
     [0.15795374]
     [0.2572828 ]
     [0.41002694]
     [0.16238885]
     [0.14137325]
     [0.06370486]
     [0.21260238]
     [0.1645925 ]
     [0.47247514]
     [0.34375778]
     [0.38453177]
     [0.23575582]
     [0.25774348]
     [0.04792204]
     [0.569437  ]
     [0.2955854 ]
     [0.27705386]
     [0.10557642]
     [0.20393282]
     [0.14561383]
     [0.05324625]
     [0.22931498]
     [0.15958767]
     [0.3626034 ]
     [0.13262722]
     [0.1618069 ]
     [0.2540402 ]
     [0.16565791]
     [0.27463427]
     [0.2831662 ]
     [0.14039251]
     [0.20909214]
     [0.07961019]
     [0.16831307]
     [0.10632644]
     [0.25511187]
     [0.09688626]
     [0.20517573]
     [0.11844373]
     [0.20770128]
     [0.14695282]
     [0.31517062]
     [0.22533385]
     [0.24190234]
     [0.16312993]
     [0.16552967]
     [0.4888069 ]
     [0.16425274]
     [0.38187623]
     [0.1650453 ]
     [0.14045842]
     [0.06928426]
     [0.22263008]
     [0.5264675 ]
     [0.28602433]
     [0.52334535]
     [0.02883768]
     [0.24169372]
     [0.07624766]
     [0.31751004]
     [0.2381358 ]
     [0.14546728]
     [0.31778744]
     [0.33224759]
     [0.3875114 ]
     [0.5812336 ]
     [0.1248336 ]
     [0.33186853]
     [0.08693488]
     [0.1027406 ]
     [0.20401256]
     [0.16501682]
     [0.257053  ]
     [0.2999533 ]
     [0.1880433 ]
     [0.4551139 ]
     [0.08139996]
     [0.24146016]
     [0.03616939]
     [0.14949146]
     [0.07616481]
     [0.19771416]
     [0.18213002]
     [0.25529233]
     [0.21642266]
     [0.32596007]
     [0.06352209]
     [0.14417818]
     [0.24210833]
     [0.31664437]
     [0.54726255]
     [0.24967906]
     [0.12247737]
     [0.02382281]
     [0.2465969 ]
     [0.33479652]
     [0.53202474]
     [0.37575385]
     [0.07474129]
     [0.08552162]
     [0.14891662]
     [0.25678536]
     [0.08189192]
     [0.08045605]
     [0.2407956 ]
     [0.42066586]
     [0.11638436]
     [0.3459558 ]
     [0.37828517]
     [0.322379  ]
     [0.14576693]
     [0.24561949]
     [0.3985174 ]
     [0.14532189]
     [0.18124813]
     [0.17489175]
     [0.2156855 ]
     [0.30889946]
     [0.14050354]
     [0.17264499]
     [0.20653333]
     [0.4559961 ]
     [0.06637115]
     [0.1834941 ]
     [0.36058605]
     [0.28912166]
     [0.369655  ]
     [0.17515448]
     [0.24777527]
     [0.28620407]
     [0.43685597]
     [0.07065373]
     [0.27370307]
     [0.20748658]
     [0.17665856]
     [0.41364956]
     [0.2475368 ]
     [0.15191613]
     [0.1673217 ]
     [0.36673826]
     [0.11719473]
     [0.2842986 ]
     [0.54968727]
     [0.31903666]
     [0.11326081]
     [0.37971175]
     [0.20474686]
     [0.50539595]
     [0.35092223]
     [0.25941768]
     [0.08607735]
     [0.19609231]
     [0.2408513 ]
     [0.17480162]
     [0.31879988]
     [0.49730992]
     [0.22114252]
     [0.12075013]
     [0.32476375]
     [0.25239417]
     [0.17155732]
     [0.14845702]
     [0.03558386]
     [0.35078603]
     [0.25448224]
     [0.26713368]
     [0.2843436 ]
     [0.44375214]
     [0.19955687]
     [0.0954298 ]
     [0.2542985 ]
     [0.13361049]
     [0.38135955]
     [0.06391115]
     [0.65258795]
     [0.21519053]
     [0.19181447]
     [0.23294452]
     [0.07242561]
     [0.0778003 ]
     [0.16877253]
     [0.24931924]
     [0.08310208]
     [0.19791771]
     [0.07280671]
     [0.25628674]
     [0.34585625]
     [0.28831047]
     [0.24650058]
     [0.17257272]
     [0.21304518]
     [0.21622375]
     [0.39263085]
     [0.03938529]
     [0.18365335]
     [0.15527907]
     [0.30346307]
     [0.49784225]
     [0.15342298]
     [0.23868872]
     [0.28039926]
     [0.13138989]
     [0.19770853]
     [0.2037362 ]
     [0.21072417]
     [0.08982147]
     [0.2661378 ]
     [0.16485366]
     [0.27717093]
     [0.1909351 ]
     [0.21820287]
     [0.10254977]
     [0.42412156]
     [0.04674362]
     [0.33249056]
     [0.3499401 ]
     [0.14735359]
     [0.5039261 ]
     [0.20162386]
     [0.1673276 ]
     [0.11144502]
     [0.21589693]
     [0.22901464]
     [0.19643717]
     [0.2680652 ]
     [0.30890402]
     [0.21406654]
     [0.16533034]
     [0.06075254]
     [0.24708393]
     [0.2639951 ]
     [0.28149787]
     [0.11919478]
     [0.24682243]
     [0.14861739]
     [0.19917142]
     [0.41258243]
     [0.1799623 ]
     [0.5735443 ]
     [0.16717482]
     [0.26629344]
     [0.11048511]
     [0.4952673 ]
     [0.4526377 ]
     [0.19535977]
     [0.06626617]
     [0.33475742]
     [0.06720635]
     [0.08385573]
     [0.1121798 ]
     [0.31087905]
     [0.12289668]
     [0.15838534]
     [0.27525762]
     [0.19710286]
     [0.02224989]
     [0.1643247 ]
     [0.18117583]
     [0.10004789]
     [0.3126958 ]
     [0.5073968 ]
     [0.06604595]
     [0.23116034]
     [0.26035616]
     [0.547022  ]
     [0.32462084]
     [0.17698197]
     [0.19240218]
     [0.38143814]
     [0.14820035]
     [0.26924554]
     [0.15207438]
     [0.17088087]
     [0.07925367]
     [0.26647896]
     [0.18787514]
     [0.27638745]
     [0.08865101]
     [0.05531118]
     [0.24228154]
     [0.19018891]
     [0.15765657]
     [0.0786178 ]
     [0.48079845]
     [0.3999899 ]
     [0.10195612]
     [0.2854506 ]
     [0.19202003]
     [0.19121581]
     [0.15751633]
     [0.46584582]
     [0.16630237]
     [0.30417612]
     [0.15639372]
     [0.33175594]
     [0.16355573]
     [0.44345146]
     [0.20871682]
     [0.12381209]
     [0.18935768]
     [0.3908467 ]
     [0.39171422]
     [0.11608682]
     [0.32409567]
     [0.30906728]
     [0.19744052]
     [0.11891425]
     [0.08129691]
     [0.40198848]
     [0.32710394]
     [0.22634101]
     [0.52401453]
     [0.21226768]
     [0.3429694 ]
     [0.25928158]
     [0.28640515]
     [0.08866087]
     [0.3528072 ]
     [0.26521546]
     [0.12900673]
     [0.14514545]
     [0.39863023]
     [0.12124092]
     [0.42681038]
     [0.18479452]
     [0.43985346]
     [0.17029096]
     [0.27200192]
     [0.07560325]
     [0.20954564]
     [0.31230366]
     [0.41816765]
     [0.2439235 ]
     [0.23260842]
     [0.05858685]
     [0.14346725]
     [0.21496671]
     [0.03333722]
     [0.12495941]
     [0.14928788]
     [0.17594676]
     [0.31654304]
     [0.07051434]
     [0.33569866]
     [0.12206015]
     [0.49229306]
     [0.41293693]
     [0.44663113]
     [0.20950605]
     [0.10715698]
     [0.25055453]
     [0.17048258]], shape=(598, 1), dtype=float32)
    tf.Tensor(
    [[2. 3. 0. ... 2. 2. 3.]
     [3. 3. 1. ... 1. 3. 1.]
     [1. 2. 0. ... 3. 3. 3.]
     ...
     [1. 3. 2. ... 3. 3. 1.]
     [1. 3. 3. ... 1. 1. 3.]
     [3. 1. 1. ... 2. 1. 2.]], shape=(598, 50), dtype=float32)
    tf.Tensor(
    [[0.9777599 ]
     [0.9863303 ]
     [0.9863275 ]
     [0.9840344 ]
     [0.8239314 ]
     [0.9940065 ]
     [0.8922725 ]
     [0.999403  ]
     [0.99125004]
     [0.8953877 ]
     [0.9940084 ]
     [0.9532425 ]
     [0.91427076]
     [0.98880184]
     [0.8724105 ]
     [0.9005273 ]
     [0.99594456]
     [0.9922369 ]
     [0.94222546]
     [0.9963691 ]
     [0.9912498 ]
     [0.9392533 ]
     [0.9229871 ]
     [0.9725041 ]
     [0.9960542 ]
     [0.99135244]
     [0.94842684]
     [0.98254573]
     [0.9071129 ]
     [0.99438715]
     [0.987165  ]
     [0.9306451 ]
     [0.9453016 ]
     [0.95491266]
     [0.9953276 ]
     [0.970555  ]
     [0.99884534]
     [0.9783959 ]
     [0.9479516 ]
     [0.9747959 ]
     [0.99654573]
     [0.9210691 ]
     [0.96842146]
     [0.99192894]
     [0.98199445]
     [0.90674204]
     [0.83208627]
     [0.8270503 ]
     [0.98264676]
     [0.97583485]
     [0.9943528 ]
     [0.9679588 ]
     [0.99765885]
     [0.9971564 ]
     [0.9989907 ]
     [0.9916459 ]
     [0.984278  ]
     [0.99109674]
     [0.9808295 ]
     [0.9887    ]
     [0.9884405 ]
     [0.998538  ]
     [0.96653104]
     [0.96877915]
     [0.9922806 ]
     [0.9813571 ]
     [0.8007392 ]
     [0.7364327 ]
     [0.994499  ]
     [0.99700874]
     [0.97632897]
     [0.990324  ]
     [0.9779218 ]
     [0.9763188 ]
     [0.95736593]
     [0.99657637]
     [0.9324984 ]
     [0.9953589 ]
     [0.99449474]
     [0.96582365]
     [0.9971596 ]
     [0.99528706]
     [0.96220684]
     [0.99656063]
     [0.99256223]
     [0.95945334]
     [0.9983317 ]
     [0.9086087 ]
     [0.98994243]
     [0.9860218 ]
     [0.9811483 ]
     [0.9553281 ]
     [0.9784676 ]
     [0.9655916 ]
     [0.98192734]
     [0.9985701 ]
     [0.99448305]
     [0.9241972 ]
     [0.99939394]
     [0.991117  ]
     [0.99153674]
     [0.8028728 ]
     [0.9891685 ]
     [0.96327865]
     [0.9630788 ]
     [0.9894051 ]
     [0.98917025]
     [0.99748516]
     [0.9941123 ]
     [0.975997  ]
     [0.99663395]
     [0.92264307]
     [0.99574006]
     [0.9820983 ]
     [0.9989594 ]
     [0.8531755 ]
     [0.99879456]
     [0.97140425]
     [0.97094846]
     [0.9884643 ]
     [0.99798286]
     [0.98691505]
     [0.99822146]
     [0.97236055]
     [0.9811528 ]
     [0.99690115]
     [0.82486504]
     [0.99475056]
     [0.9992668 ]
     [0.9933828 ]
     [0.96602607]
     [0.8932046 ]
     [0.98856944]
     [0.9960951 ]
     [0.9341425 ]
     [0.99837166]
     [0.93498296]
     [0.99180496]
     [0.9807356 ]
     [0.9980598 ]
     [0.9796037 ]
     [0.92678374]
     [0.9793305 ]
     [0.987591  ]
     [0.99503505]
     [0.9988784 ]
     [0.99335396]
     [0.97802347]
     [0.9722519 ]
     [0.9125066 ]
     [0.8964589 ]
     [0.98640984]
     [0.9686915 ]
     [0.9978162 ]
     [0.91813624]
     [0.99645376]
     [0.9920183 ]
     [0.99983287]
     [0.8925005 ]
     [0.9978204 ]
     [0.99924505]
     [0.9894785 ]
     [0.89713067]
     [0.9913394 ]
     [0.9960069 ]
     [0.9949774 ]
     [0.71827006]
     [0.99683326]
     [0.99135673]
     [0.99712425]
     [0.9572756 ]
     [0.9993506 ]
     [0.9978801 ]
     [0.9995074 ]
     [0.9905702 ]
     [0.9231906 ]
     [0.98951143]
     [0.7717949 ]
     [0.92293733]
     [0.9967217 ]
     [0.7655146 ]
     [0.9584887 ]
     [0.99363106]
     [0.9910883 ]
     [0.96778464]
     [0.98796797]
     [0.9874072 ]
     [0.8671845 ]
     [0.9923649 ]
     [0.99690706]
     [0.99395233]
     [0.98612845]
     [0.93725884]
     [0.9927187 ]
     [0.9883956 ]
     [0.9536047 ]
     [0.9971475 ]
     [0.9873351 ]
     [0.9841576 ]
     [0.9501855 ]
     [0.9016285 ]
     [0.98901165]
     [0.99162394]
     [0.985911  ]
     [0.9994116 ]
     [0.9840099 ]
     [0.9896926 ]
     [0.99390465]
     [0.9837737 ]
     [0.9879389 ]
     [0.9849358 ]
     [0.95444214]
     [0.96185106]
     [0.98583764]
     [0.9853887 ]
     [0.96530604]
     [0.9577175 ]
     [0.99209654]
     [0.99182796]
     [0.9970042 ]
     [0.96313506]
     [0.99665725]
     [0.99876225]
     [0.9759604 ]
     [0.9753566 ]
     [0.94357455]
     [0.9939639 ]
     [0.9975822 ]
     [0.9581607 ]
     [0.7264057 ]
     [0.90340513]
     [0.990231  ]
     [0.88551056]
     [0.9924867 ]
     [0.8890426 ]
     [0.9955445 ]
     [0.9477519 ]
     [0.9626575 ]
     [0.8927224 ]
     [0.9731145 ]
     [0.95368886]
     [0.9925754 ]
     [0.96909136]
     [0.9865371 ]
     [0.99098164]
     [0.99682367]
     [0.9978574 ]
     [0.9680195 ]
     [0.9926634 ]
     [0.9746379 ]
     [0.9966265 ]
     [0.9998901 ]
     [0.9713291 ]
     [0.9950132 ]
     [0.9958442 ]
     [0.76255816]
     [0.99455076]
     [0.99374866]
     [0.9055506 ]
     [0.98487365]
     [0.98789865]
     [0.9974554 ]
     [0.9921153 ]
     [0.99307597]
     [0.9962943 ]
     [0.99538213]
     [0.91977525]
     [0.97862715]
     [0.9989808 ]
     [0.98903465]
     [0.98844856]
     [0.97251344]
     [0.9740209 ]
     [0.99541867]
     [0.9888009 ]
     [0.9933815 ]
     [0.9661086 ]
     [0.97533935]
     [0.99521685]
     [0.8713212 ]
     [0.9987118 ]
     [0.9930655 ]
     [0.9917338 ]
     [0.9680951 ]
     [0.9979837 ]
     [0.9833829 ]
     [0.9975721 ]
     [0.9794351 ]
     [0.99949396]
     [0.99854004]
     [0.9841129 ]
     [0.9975897 ]
     [0.8182254 ]
     [0.9975799 ]
     [0.987058  ]
     [0.96534294]
     [0.9412237 ]
     [0.97617936]
     [0.96353686]
     [0.99826896]
     [0.99806195]
     [0.99664634]
     [0.99624157]
     [0.83291   ]
     [0.96318394]
     [0.99731857]
     [0.9907126 ]
     [0.98717207]
     [0.9940282 ]
     [0.9981206 ]
     [0.96425235]
     [0.99548596]
     [0.73197335]
     [0.98325104]
     [0.9797626 ]
     [0.8972077 ]
     [0.9165122 ]
     [0.99880195]
     [0.9910808 ]
     [0.99641883]
     [0.9984005 ]
     [0.9972716 ]
     [0.9597902 ]
     [0.99109954]
     [0.94714653]
     [0.9839405 ]
     [0.98746014]
     [0.8297749 ]
     [0.98105836]
     [0.9527131 ]
     [0.9817244 ]
     [0.99557894]
     [0.90665406]
     [0.9853733 ]
     [0.9990928 ]
     [0.9860756 ]
     [0.97990465]
     [0.97841316]
     [0.99897885]
     [0.9977437 ]
     [0.997695  ]
     [0.9901085 ]
     [0.8306896 ]
     [0.96802527]
     [0.971312  ]
     [0.72074664]
     [0.8460156 ]
     [0.9898733 ]
     [0.9975541 ]
     [0.9869484 ]
     [0.97605443]
     [0.9917005 ]
     [0.9050897 ]
     [0.99558026]
     [0.98022455]
     [0.9734471 ]
     [0.994484  ]
     [0.98737013]
     [0.99581414]
     [0.9929905 ]
     [0.8427623 ]
     [0.76278067]
     [0.98167545]
     [0.99067265]
     [0.97734493]
     [0.9303689 ]
     [0.9639088 ]
     [0.9192448 ]
     [0.9721788 ]
     [0.87672526]
     [0.99951243]
     [0.9984988 ]
     [0.99651706]
     [0.89290285]
     [0.9909025 ]
     [0.99884474]
     [0.99123925]
     [0.9986527 ]
     [0.99287874]
     [0.9690683 ]
     [0.9606018 ]
     [0.9375396 ]
     [0.99837434]
     [0.9916848 ]
     [0.98857087]
     [0.879761  ]
     [0.98254323]
     [0.95356655]
     [0.9961041 ]
     [0.9931426 ]
     [0.9542254 ]
     [0.9927912 ]
     [0.9568587 ]
     [0.96016794]
     [0.68044466]
     [0.9977792 ]
     [0.9692128 ]
     [0.99516135]
     [0.9218072 ]
     [0.99541086]
     [0.9970843 ]
     [0.99882776]
     [0.97770965]
     [0.9805741 ]
     [0.91887116]
     [0.99754435]
     [0.9462496 ]
     [0.9954247 ]
     [0.97386116]
     [0.8984086 ]
     [0.98214906]
     [0.8711413 ]
     [0.99081236]
     [0.9771293 ]
     [0.9918087 ]
     [0.8415312 ]
     [0.98301184]
     [0.9952857 ]
     [0.962767  ]
     [0.9556806 ]
     [0.9336488 ]
     [0.9763722 ]
     [0.99564594]
     [0.9526287 ]
     [0.97115874]
     [0.99972063]
     [0.91972774]
     [0.99254954]
     [0.99420375]
     [0.9913105 ]
     [0.992679  ]
     [0.97849965]
     [0.96279484]
     [0.96888834]
     [0.9660808 ]
     [0.9893729 ]
     [0.92404985]
     [0.9280869 ]
     [0.99440974]
     [0.98688877]
     [0.9745617 ]
     [0.9621764 ]
     [0.9850927 ]
     [0.9857566 ]
     [0.93095064]
     [0.9977549 ]
     [0.9812232 ]
     [0.9126662 ]
     [0.9566443 ]
     [0.8646174 ]
     [0.50228536]
     [0.9522517 ]
     [0.96511346]
     [0.9949055 ]
     [0.9948192 ]
     [0.9600249 ]
     [0.9916999 ]
     [0.9961384 ]
     [0.97903305]
     [0.9978885 ]
     [0.9987017 ]
     [0.9840058 ]
     [0.99888045]
     [0.9568232 ]
     [0.96701294]
     [0.99321824]
     [0.9512193 ]
     [0.99558455]
     [0.96378917]
     [0.9913681 ]
     [0.9959472 ]
     [0.95682937]
     [0.9726681 ]
     [0.9287377 ]
     [0.9847638 ]
     [0.9635327 ]
     [0.9574488 ]
     [0.9904744 ]
     [0.9894666 ]
     [0.97807115]
     [0.9650212 ]
     [0.9982539 ]
     [0.9924304 ]
     [0.8373775 ]
     [0.9977938 ]
     [0.9845254 ]
     [0.9694584 ]
     [0.98908806]
     [0.97541624]
     [0.9640077 ]
     [0.9721547 ]
     [0.906111  ]
     [0.9784305 ]
     [0.98105145]
     [0.9368743 ]
     [0.9953949 ]
     [0.9806919 ]
     [0.97262716]
     [0.908168  ]
     [0.8312944 ]
     [0.9938699 ]
     [0.99518436]
     [0.99618894]
     [0.9482542 ]
     [0.99034435]
     [0.9455495 ]
     [0.88142455]
     [0.974901  ]
     [0.95971745]
     [0.9652954 ]
     [0.95488346]
     [0.99204975]
     [0.99632096]
     [0.9414955 ]
     [0.99402076]
     [0.9972193 ]
     [0.97236496]
     [0.9111573 ]
     [0.997451  ]
     [0.97536236]
     [0.9942204 ]
     [0.9766573 ]
     [0.99488926]
     [0.9934136 ]
     [0.9972235 ]
     [0.9821973 ]
     [0.77461237]
     [0.9422089 ]
     [0.99545044]
     [0.9571677 ]
     [0.9987326 ]
     [0.98533547]
     [0.9930688 ]
     [0.9769747 ]
     [0.9558447 ]
     [0.96666   ]
     [0.99129736]
     [0.77438045]
     [0.9905688 ]
     [0.91357446]
     [0.9675626 ]
     [0.99785405]
     [0.9983891 ]
     [0.9875373 ]
     [0.99442273]
     [0.98508203]
     [0.9974554 ]
     [0.99498147]
     [0.9937528 ]
     [0.9844578 ]
     [0.99076146]
     [0.9901904 ]
     [0.96884435]
     [0.98003656]
     [0.9905089 ]
     [0.9816591 ]
     [0.9902226 ]
     [0.9891377 ]
     [0.7547166 ]
     [0.93586457]
     [0.9727118 ]
     [0.98243284]
     [0.993082  ]
     [0.986894  ]
     [0.97965264]
     [0.98201525]
     [0.98979354]
     [0.95748913]
     [0.9981375 ]
     [0.98155767]
     [0.96112615]
     [0.9385618 ]
     [0.98897713]
     [0.9958942 ]
     [0.98520434]
     [0.99545336]
     [0.84992796]
     [0.96527183]
     [0.98988694]
     [0.9977718 ]
     [0.9738715 ]
     [0.9973437 ]
     [0.9461605 ]
     [0.991856  ]
     [0.99376464]
     [0.9867771 ]
     [0.99058175]
     [0.98116404]
     [0.9940236 ]
     [0.7476264 ]
     [0.90638494]
     [0.9893499 ]
     [0.9479379 ]
     [0.9984478 ]
     [0.9630943 ]
     [0.9964647 ]
     [0.98212415]
     [0.9980027 ]], shape=(598, 1), dtype=float32)

# Evaluate model

```
def get_eval_metric_ops_fn(gan_model):
  real_data_logits = tf.reduce_mean(gan_model.discriminator_real_outputs)
  gen_data_logits = tf.reduce_mean(gan_model.discriminator_gen_outputs)

  # Attempt to calculate entropy to see how accurate the network is
  # (but this doesn't work yet)
  cce = tf.keras.losses.CategoricalCrossentropy()
  entropy = cce(gan_model.generated_data, gan_model.real_data)
  return {
      'real_data_logits': tf.compat.v1.metrics.mean(real_data_logits),
      'gen_data_logits': tf.compat.v1.metrics.mean(gen_data_logits),
      'entropy':tf.compat.v1.metrics.mean(entropy),
  }
```

# GANEstimator

```
tf.get_logger().setLevel('INFO')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
tf.autograph.set_verbosity(0, False)

generator_lr = 0.001
discriminator_lr = 0.0002

# Configure the GAN estimator with all the functions from above
gan_estimator = tfg.estimator.GANEstimator(
    generator_fn=quantum_generator_model,
    discriminator_fn=discriminator_model,
    generator_loss_fn=tfg.losses.modified_generator_loss,
    discriminator_loss_fn=tfg.losses.modified_discriminator_loss,
    generator_optimizer=tf.compat.v1.train.AdamOptimizer(generator_lr),
    discriminator_optimizer=tf.compat.v1.train.AdamOptimizer(discriminator_lr),
    get_eval_metric_ops_fn=get_eval_metric_ops_fn)

steps_per_eval =  500#@param
max_train_steps =  5000#@param
batches_for_eval_metrics = 100 #@param

# Used to track metrics.
steps = []
real_logits, fake_logits = [], []
frequencies = []
entropy = []

cur_step = 0
while cur_step < max_train_steps:
  next_step = min(cur_step + steps_per_eval, max_train_steps)
  gan_estimator.train(generate_data, max_steps=next_step)
  steps_taken = next_step - cur_step
  cur_step = next_step

  # Calculate some metrics.
  metrics = gan_estimator.evaluate(generate_data,
                                   steps=batches_for_eval_metrics)

  # Generate predictions
  iterator = gan_estimator.predict(generate_data)
  predictions = np.array([next(iterator) for _ in range(10)])
  frequency = np.mean(
      [np.bincount(p.astype(int), minlength=4) for p in predictions], axis=0)
  print(frequency)
  steps.append(cur_step)
  real_logits.append(metrics['real_data_logits'])
  fake_logits.append(metrics['gen_data_logits'])
  print('Average discriminator output on Real: %.2f  Fake: %.2f' % (
      real_logits[-1], fake_logits[-1]))
  plt.figure()
  plt.bar(np.arange(0,4), frequency)
  frequencies.append(frequency)
  entropy.append(metrics['entropy'])

plt.figure()
plt.plot(steps, frequencies)
plt.figure()
plt.plot(steps, entropy)
```