How do I make graph(E vs k) for a given hamiltonian

I am making band structure of some material using ssvqe. I made hamiltonian of the material using some physical method . Then decomposed it to make qubit hamiltonian . Then I used ssvqe algorithm to get excited state energies for some k values( for various high symmetry points in brillouin zone) . But I need to make graph between E vs k . For this how can I define k and how can I run SSVQE on it?

# Put code here

If you want help with diagnosing an error, please put the full error message below:

# Put full error message here

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Hi @Kaushik! Are you able to share some PennyLane code or provide a few more details about what you’re trying to do? You can also check out our quantum chemistry demos.

import pennylane as qml

def k_dependent_hamiltonian(k_vector, params):
    kx, ky, kz = k_vector

    
    gamma1 = params[0]  
    gamma2 = params[1] 
    gamma3 = params[2]  

    
    P = params[3]  

    
    H0 = gamma1 * qml.PauliZ(0) + gamma2 * (qml.PauliZ(1) + qml.PauliZ(2))
    Hk = (
        kx**2 * (gamma1 - 2 * gamma2 / 3) * qml.PauliZ(0)
        + ky**2 * (gamma1 + gamma2 / 3) * qml.PauliZ(1)
        + kz**2 * (gamma1 + gamma2 / 3) * qml.PauliZ(2)
    )
    Hso = -P / (2 * m0) * (
        kx * qml.PauliX(0) + ky * qml.PauliY(1) + kz * qml.PauliY(2)
    )  

    return qml.Hamiltonian(H0 + Hk + Hso)
import pennylane as qml
import numpy as np
import matplotlib.pyplot as plt


k_path = np.array([[0, 0, 0], [0.5, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0]])

lk_params = [6.98, 2.06, 2.93]
kane_params = [10.5]

def k_dependent_hamiltonian(k_vector,params):
     kx, ky, kz = k_vector

    
    gamma1 = params[0]  
    gamma2 = params[1]  
    gamma3 = params[2]  

    
    P = params[3]  

    
    H0 = gamma1 * qml.PauliZ(0) + gamma2 * (qml.PauliZ(1) + qml.PauliZ(2))
    Hk = (
        kx**2 * (gamma1 - 2 * gamma2 / 3) * qml.PauliZ(0)
        + ky**2 * (gamma1 + gamma2 / 3) * qml.PauliZ(1)
        + kz**2 * (gamma1 + gamma2 / 3) * qml.PauliZ(2)
    )
    Hso = -P / (2 * m0) * (
        kx * qml.PauliX(0) + ky * qml.PauliY(1) + kz * qml.PauliY(2)
    )  

    return qml.Hamiltonian(H0 + Hk + Hso)

def ansatz(params, wires):
    
    qml.RY(params[0], wires=0)
    qml.RX(params[1], wires=0)

@qml.qnode(device)
def cost_function(params, k_vector):
    energy = qml.expval(k_dependent_hamiltonian(k_vector), params)
    return energy**2  

def ssvqe(k_path, hamiltonian, ansatz, num_qubits, opt_name='Adam', stepsize=0.1, iterations=100):
    device = qml.device('default.qubit', wires=num_qubits)
    energies = []

    for k_vector in k_path:
        opt = qml.qml.optimize(opt_name, stepsize=stepsize)
        params = np.random.uniform(0, np.pi, size=2)  

        for _ in range(iterations):
            params, _ = opt.step(lambda p: cost_function(p, k_vector), params)

        energy = qml.expval(hamiltonian(k_vector), params)
        energies.append(energy)

    return energies


num_qubits = 2


band_energies = ssvqe(k_path, k_dependent_hamiltonian, ansatz, num_qubits)


plt.plot(k_path[:, 0], band_energies, label='SSVQE (Sample)')
plt.xlabel('k_x')
plt.ylabel('Energy (eV)')
plt.title('Sample GaAs Band Structure')
plt.legend()
plt.show()

I hope this code will make my question clear. I got this code from google.

Hi @Kaushik. Your code fails because of some small typos and issues that I mention in the following:

  • The code uses qml.qml in line 75. Please also see this demo and the example here for more information on using optimizers.

  • Please separate your cost function from the qnode, similar to the code examples in this demo.

  • I also noticed that the k_dependent_hamiltonian function is called with only one argument in your code, while it accepts two.

I hope these help to make your code run. Please let us know if you have further questions!