About quantum natural gradient circuit


In the article “Quantum natural gradient”, for three qubits we have two parametrized gates in Parametrized Layer_0,

  1. Is the wire choosen randomly to apply RY operations?

  2. If the input layer has four qubits, then Parametrized Layer_0 must contains totally three qubits each one on wire_1, wire_2 and wire_3?

Your Sincerely

Hi @Franz_Ogur!

Is the wire chosen randomly to apply RY operations?

In a way, yes. Every wire has a parametrized rotation gate applied, but the specific gate is chosen at random from the set \{RX, RY, RZ\}.

The PennyLane code that creates the circuit explored in the paper is:

gates = [
    [np.random.choice([qml.RX, qml.RY, qml.RZ]) for _ in range(depth)] for _ in range(n_qubits)

def Ry_layer():
    for wire in range(n_qubits):
        qml.RY(np.pi / 4, wires=wire)

def parametric_layer(params, layer):
    for wire in range(n_qubits):
        gates[wire][layer](params[wire, layer], wires=wire)

def entangler():
    for wire in range(0, n_qubits - 1, 2):
        qml.CZ(wires=[wire, wire + 1])
    for wire in range(1, n_qubits, 2):
        qml.CZ(wires=[wire, wire + 1])

def circuit(params):
    params = np.reshape(params, (n_qubits, depth))
    for layer in range(depth):
        parametric_layer(params, layer)
    return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

Hi @josh,

Thank you for your reply.

According to your code below, every wires in every layers has a gate. But in the picture below at ParametrizedLayer_0, I see that RZ gates applied only on wire of 0 and 1. Is there a confliction between code and the picture?

Hi @Franz_Ogur, yes the code doesn’t correspond to the picture.

The code is the same used to create the variational circuit explored in the Quantum Natural Gradient paper.

For the QNG tutorial, I created a simplified version of the circuit (including the gaps you see) just to make it easier to discuss :slightly_smiling_face:

Dear @josh,

Is it possible to use QNG algorithm as a supervised learning algorithm?
I mean, I have a dataset with 90K rows and each row has five columns as features and the sixth column is the target value. So in your opinion, how to modify the bellow “for loop” in the QNG example?

opt = qml.QNGOptimizer(0.01)
theta = init_params
for _ in range(steps):
    theta = opt.step(circuit, theta)

Hi @Franz_Ogur,

Note that the quantum natural gradient is an optimization strategy, and not part of the model per se. You can use any optimization strategy for a supervised learning algorithm, it’s simply a matter of constructing the cost function appropriately.

For an example of a quantum supervised learning example, check out our quantum transfer learning tutorial. Here, we use the nn.CrossEntropyLoss cost function from PyTorch for training our model.

Something to be aware of, however, is that the QNG optimizer currently requires that the cost function be a linear combination of QNodes with the same ansatz. It’s possible to extend this to more complicated cost functions, and something we are currently working on!

Hi @josh,

Thank you so much for the supports. I try a way to use VQECost function but Hamiltonian parameter of the function is difficult to understand for me.

cost = qml.VQECost(ansatz, hamiltonian, dev)

Could you please make it clear, how the following equation is expressed? Are Pauli operators and the cofficients choosen randomly?

Hi @Franz_Ogur, in this particular example, we want to construct the Hamiltonian

H = 0.5Y_2 + 0.8Z_1-0.2X_1

which corresponds to

coeffs = [0.5, 0.8, -0.2]
obs = [qml.PauliY(2), qml.PauliZ(1), qml.PauliX(1)]
H = qml.Hamiltonian(coeffs, obs)


So, elements of coeffs array determined randomly?
What is the rule of randomness? Is it always in [-1, 1]?

So, elements of coeffs array determined randomly?

In general, the coefficients of the Hamiltonian in the model are problem specific. For example, in VQE, you have a specific molecule or electronic structure that you wish to compute the ground state energy; this corresponds to a specific Hamiltonian with hardcoded coefficients.

You can check also check out our quantum chemistry demo for more details on solving electronic structure problems using PennyLane!

Having said that, there may be some problems/models where the exact Hamiltonian doesn’t matter too much, and the coefficients can be chosen randomly :slightly_smiling_face: In such a case, any real value is allowed for the coefficients!