Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qiboml tensorflow vs old qibo versions #102

Open
marekgluza opened this issue Nov 26, 2024 · 0 comments
Open

qiboml tensorflow vs old qibo versions #102

marekgluza opened this issue Nov 26, 2024 · 0 comments

Comments

@marekgluza
Copy link
Contributor

I was trying to figure out how to run the vqe and I think my issue
qiboteam/qibo#1530
was that tensorflow was moved to qiboml?

I tried

def vqe_loss(params, circuit, hamiltonian, nshots=None):
    """Evaluate the hamiltonian expectation values of the circuit final state."""
    circ = circuit.copy(deep=True)
    circ.set_parameters(params)
    if isinstance(hamiltonian.backend, qiboml.backends.tensorflow.TensorflowBackend) and nshots is not None:
        expectation_value = _exp_with_tf(
            circuit=circ, hamiltonian=hamiltonian, nshots=nshots
        )
    elif nshots is None:
        expectation_value = _exact(circ, hamiltonian)
    else:
        expectation_value = _with_shots(circ=circ, ham=hamiltonian, nshots=nshots)
    return expectation_value

and

def compute_gradients(parameters, circuit, hamiltonian):
    """
    Compute gradients of circuit's parameters to check the problem trainability.
    The evaluated derivatives are the ones of the expectation of `hamiltonian`
    over the final state get running `circuit.execute` w.r.t. rotational angles.

    """

    qibo.set_backend(backend="qiboml", platform="tensorflow")
    tf_backend = qibo.get_backend()
    parameters = tf_backend.tf.Variable(parameters, dtype=tf_backend.tf.float64)
    with tf_backend.tf.GradientTape() as tape:
        circuit.set_parameters(parameters)
        final_state = tf_backend.execute_circuit(circuit).state()
        expectation = tf_backend.calculate_expectation_state(
            tf_backend.cast(hamiltonian.matrix), final_state, normalize=False
        )

    return hamiltonian.backend.cast(tape.gradient(expectation, parameters))

but it sgd didn't run so I'm trying cma instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant