Hello! If applicable, put your complete code example down below. Make sure that your code:
- is 100% self-contained — someone can copy-paste exactly what is here and run it to
reproduce the behaviour you are observing - includes comments
def create_hamiltonian_matrix(n_qubits, graph, weights, bias):
full_matrix = np.zeros((2 ** n_qubits, 2 ** n_qubits))
# Creates the interaction component of the Hamiltonian
for i, edge in enumerate(graph.edges):
interaction_term = 1
for qubit in range(0, n_qubits):
if qubit in edge:
interaction_term = np.kron(interaction_term, qml.matrix(qml.PauliZ)(0))
else:
interaction_term = np.kron(interaction_term, np.identity(2))
full_matrix += weights[i] * interaction_term
# Creates the bias components of the matrix
for i in range(0, n_qubits):
z_term = x_term = 1
for j in range(0, n_qubits):
if j == i:
z_term = np.kron(z_term, qml.matrix(qml.PauliZ)(0))
x_term = np.kron(x_term, qml.matrix(qml.PauliX)(0))
else:
z_term = np.kron(z_term, np.identity(2))
x_term = np.kron(x_term, np.identity(2))
full_matrix += bias[i] * z_term + x_term
return full_matrix
# Prints a visual representation of the Hamiltonian matrix
ham_matrix = create_hamiltonian_matrix(qubit_number, ising_graph, target_weights, target_bias)
plt.matshow(ham_matrix, cmap="hot")
plt.show()
If you want help with diagnosing an error, please put the full error message below:
ttributeError Traceback (most recent call last)
Cell In[12], line 31
27 return full_matrix
30 # Prints a visual representation of the Hamiltonian matrix
---> 31 ham_matrix = create_hamiltonian_matrix(qubit_number, ising_graph, target_weights, target_bias)
32 plt.matshow(ham_matrix, cmap="hot")
33 plt.show()
Cell In[12], line 10, in create_hamiltonian_matrix(n_qubits, graph, weights, bias)
8 for qubit in range(0, n_qubits):
9 if qubit in edge:
---> 10 interaction_term = np.kron(interaction_term, qml.matrix(qml.PauliZ)(0))
11 else:
12 interaction_term = np.kron(interaction_term, np.identity(2))
AttributeError: module 'pennylane' has no attribute 'matrix'
And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about()
.
Version: 0.18.0