qiskit-community / qiskit-machine-learning

Quantum Machine Learning
https://qiskit-community.github.io/qiskit-machine-learning/
Apache License 2.0
695 stars 327 forks source link

Gradient Descent in qGAN optimisation #383

Closed ebermot closed 1 year ago

ebermot commented 2 years ago

Environment

What is happening?

I'm trying to run an optimisation of the qGAN model. However, when I try to change the generator optimizer from ADAM to GradientDescent, I get an error during the qgan.run(quantum_instance) : "[Expression cannot bind non-numeric values ({ParameterVectorElement(θ[0]): array([-5.15174274e-05, -4.29241330e-05, -4.65541899e-05, -5.62154682e-05,\n -5.74424778e-05, -4.81389433e-05, -5.85206010e-05, -5.40145357e-05,\n -4.95023381e-05, -5.36247832e-05, -6.18140874e-05, -4.41827414e-05])})]()"

How can we reproduce the issue?


seed=23
np.random.seed(seed)

from qiskit import QuantumRegister, QuantumCircuit, BasicAer
from qiskit.circuit.library import TwoLocal

from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit_machine_learning.algorithms import NumPyDiscriminator, QGAN
from qiskit.algorithms.optimizers import ADAM, Optimizer, GradientDescent
algorithm_globals.random_seed = seed

# Number training data samples
N = 20000

# Load data samples from log-normal distribution with mean=1 and standard deviation=1
mu = 1
sigma = 1
real_data = np.random.lognormal(mean=mu, sigma=sigma, size=N)

# Set the data resolution
# Set upper and lower data values as list of k min/max data values [[min_0,max_0],...,[min_k-1,max_k-1]]
bounds = np.array([0, 7.0])
# Set number of qubits per data dimension as list of k qubit values[#q_0,...,#q_k-1]
num_qubits = [3]
k = len(num_qubits)

# Set number of training epochs
# Note: The algorithm's runtime can be shortened by reducing the number of training epochs.
num_epochs = 2000
# Batch size
batch_size = 2000

# Initialize qGAN
qgan = QGAN(real_data, bounds, num_qubits, batch_size, num_epochs, snapshot_dir=None)
qgan.seed = 12
# Set quantum instance to run the quantum generator
quantum_instance = QuantumInstance(
    backend=BasicAer.get_backend("statevector_simulator"), seed_transpiler=seed, seed_simulator=seed
)

# Set an initial state for the generator circuit as a uniform distribution
# This corresponds to applying Hadamard gates on all qubits
init_dist = QuantumCircuit(sum(num_qubits))
init_dist.h(init_dist.qubits)

# Set the ansatz circuit
ansatz = TwoLocal(int(np.sum(num_qubits)), "ry", "cz", entanglement='circular', reps=3)

# Set generator's initial parameters - in order to reduce the training time and hence the
# total running time for this notebook
init_params = np.random.uniform(low=-1e-1, high=1e-1, size=ansatz.num_parameters_settable)
# Set generator circuit by adding the initial distribution infront of the ansatz
g_circuit = ansatz.compose(init_dist, front=True)

# Set quantum generator

generator_optimizer=GradientDescent(maxiter=1, learning_rate=1e-3)
qgan.set_generator(generator_circuit=g_circuit, generator_init_params=init_params, generator_optimizer=generator_optimizer)
# The parameters have an order issue that following is a temp. workaround
qgan._generator._free_parameters = sorted(g_circuit.parameters, key=lambda p: p.name)
# Set classical discriminator neural network
discriminator = NumPyDiscriminator(len(num_qubits))
qgan.set_discriminator(discriminator)

result = qgan.run(quantum_instance)

What should happen?

It should optimize, instead of outputting the error.

Any suggestions?

No response

adekusar-drl commented 2 years ago

The QGAN implementation is deprecated in https://github.com/Qiskit/qiskit-machine-learning/pull/495. Unlikely this issue will be fixed.

adekusar-drl commented 1 year ago

Closing this issue as the original QGAN implementation has been removed.