Giter VIP home page Giter VIP logo

Comments (9)

austingmhuang avatar austingmhuang commented on August 25, 2024 1

Update:

After some investigation with the help of @soranjh, it appears that the error is raised if the given Hamiltonian has an identity term with a non-zero coefficient.

E.g.

import pennylane as qml
from pennylane.optimize.adaptive import append_gate
from pennylane import numpy as pnp

coeffs = pnp.array([0.2, -0.543, 1], requires_grad=False)
obs = [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.X(1), qml.I(0)]
H = qml.ops.LinearCombination(coeffs, obs)

operator_pool = [qml.CRX(0.0, [0, 1]), qml.CRY(0.0, [1, 2])]

dev = qml.device("default.qubit", wires=10)

@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="adjoint")
def circuit():
    for i in range(3):
        qml.RX(0.5, i)

    return qml.expval(H)

circuit()
params = pnp.array([0.1, 0.2], requires_grad=True)
qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)

A proposed solution is in #6022

from pennylane.

albi3ro avatar albi3ro commented on August 25, 2024

Note that the issue is not particularily with the qulacs device, but with split_non_commuting.

Sticking qml.transforms.split_non_commuting onto the qnode causes the same error.

from pennylane.optimize.adaptive import append_gate
from pennylane import numpy as np

H, qubits = qml.qchem.molecular_hamiltonian(
    ["Li", "H"],
    np.array([0.0, 0.0, 0.0, 0.0, 0.0, 2.969280527]),
    active_electrons=2,
    active_orbitals=5
)

operator_pool = [qml.CRX(0.0, [0, 1]), qml.CRY(0.0, [1, 2])]

dev = qml.device("default.qubit", wires=10)

@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="parameter-shift")
def circuit():
    for i in range(3):
        qml.RX(0.5, i)

    return qml.expval(H)

params = np.array([0.1, 0.2], requires_grad=True)
qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)

from pennylane.

albi3ro avatar albi3ro commented on August 25, 2024

This may also be related to #5824

from pennylane.

josh146 avatar josh146 commented on August 25, 2024

Thanks @albi3ro! I suspected it was something to do with the Hamiltonian, but didn't realize it was because H had non-commuting terms.

Is the bug present with just split_non_commuting and non-H-commuting-supporting-devices + grad? In which case, we could remove the append_gate transform from the minimal error example

from pennylane.

albi3ro avatar albi3ro commented on August 25, 2024

It has less to do with the qulacs simulator, and more about a device that doesn't natively support hamiltonians.

Why append_gate causes this, i have no idea.

from pennylane.

josh146 avatar josh146 commented on August 25, 2024

oops, typo in my message, have updated it!

from pennylane.

albi3ro avatar albi3ro commented on August 25, 2024

Never mind. This has nothing to do with append_gate.

The issue can also be replicated with:

H = pnp.array(0.5, requires_grad=False) * qml.Y(0)

dev = qml.device("default.qubit", wires=10)

@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="adjoint")
def circuit(x):
    qml.RX(x, 0)
    return qml.expval(H)
params = pnp.array(0.1, requires_grad=True)
qml.grad(circuit)(params)

from pennylane.

josh146 avatar josh146 commented on August 25, 2024

Thanks, that's great! Much more minimal now

from pennylane.

austingmhuang avatar austingmhuang commented on August 25, 2024

Sorry for re-opening this issue. The original code block (w/ minor changes) still raises a NonDifferentiableError:

import pennylane as qml
from pennylane.optimize.adaptive import append_gate
from pennylane import numpy as np

H, qubits = qml.qchem.molecular_hamiltonian(
    ["Li", "H"],
    np.array([0.0, 0.0, 0.0, 0.0, 0.0, 2.969280527]),
    active_electrons=2,
    active_orbitals=5
)

operator_pool = [qml.CRX(0.0, [0, 1]), qml.CRY(0.0, [1, 2])]

dev = qml.device("default.qubit", wires=10)

@qml.transforms.split_non_commuting
@qml.qnode(dev, diff_method="adjoint")
def circuit():
    for i in range(3):
        qml.RX(0.5, i)

    return qml.expval(H)

params = np.array([0.1, 0.2], requires_grad=True)
qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)

Error:

>>> qml.grad(lambda p: append_gate(circuit, p, operator_pool)())(params)
NonDifferentiableError: -6.917117171256823 is non-differentiable. Set the requires_grad attribute to True.

from pennylane.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.