Skip to content

Mismatch array size for large t #3

@flynnbr11

Description

@flynnbr11

When using large t and large Hamiltonians (observed for Hamiltonians with >5 qubits, t>10), the output of the function becomes an np.matrix instead of an np.array.
Consequently, np.dot() using the output matrix will crash for large t where it worked for small t.
e.g. the following will work for t=small_t and crash for t=large_t:

import hamiltonian_exponentiation as h
import numpy as np

small_t = 1
large_t = 15
some_hamiltonian = h.random_hamiltonian(5)
initial_state = np.array([1,0])

# This works:
U = h.exp_ham(some_hamiltonian, t=small_t)
evolved_state = np.dot(U, initial_state)

# This crashes:
U = h.exp_ham(some_hamiltonian, t=large_t)
evolved_state = np.dot(U, initial_state)

The crash will be of type

ValueError: shapes (64,) and (1,64) not aligned: 64 (dim 0) != 1 (dim 0)

becasue U is an np.matrix with shape (64, 64), whereas in order for np.dot to work while the second argument is an np.array with shape (64, ), the first argument also needs to be an np.array of shape (64, 64), i.e. not a matrix.

This will be fixed in the next update, but can be fixed by setting:

U = np.array(h.exp_ham(some_hamiltonian, t=large_t))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions