Skip to content

qiboml tensorflow vs old qibo versions #102

@marekgluza

Description

@marekgluza

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions