Skip to content

Commit

Permalink
Merge pull request qutip#1688 from AGaliciaMartinez/hadamard_transfor…
Browse files Browse the repository at this point in the history
…m_efficient

Improve Hadamard transform efficiency by up to ~70x.
  • Loading branch information
hodgestar committed Feb 4, 2022
1 parent b35e2d2 commit 5642874
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions qutip/qip/operations/gates.py
Expand Up @@ -924,11 +924,9 @@ def hadamard_transform(N=1):
Quantum object representation of the N-qubit Hadamard gate.
"""
data = 2 ** (-N / 2) * np.array([[(-1) ** _hamming_distance(i & j)
for i in range(2 ** N)]
for j in range(2 ** N)])
H = Qobj([[1, 1], [1, -1]]) / np.sqrt(2)

return Qobj(data, dims=[[2] * N, [2] * N])
return tensor([H] * N)


def _flatten(lst):
Expand Down
14 changes: 14 additions & 0 deletions qutip/tests/test_gates.py
Expand Up @@ -332,6 +332,20 @@ def test_cnot_explicit(self):
[0, 0, 0, 1, 0, 0, 0, 0]])
np.testing.assert_allclose(test, expected, atol=1e-15)


def test_hadamard_explicit(self):
test = gates.hadamard_transform(3).full()
expected = np.array([[ 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, -1, 1, -1, 1, -1, 1, -1],
[ 1, 1, -1, -1, 1, 1, -1, -1],
[ 1, -1, -1, 1, 1, -1, -1, 1],
[ 1, 1, 1, 1, -1, -1, -1, -1],
[ 1, -1, 1, -1, -1, 1, -1, 1],
[ 1, 1, -1, -1, -1, -1, 1, 1],
[ 1, -1, -1, 1, -1, 1, 1, -1]])
expected = expected/np.sqrt(8)
np.testing.assert_allclose(test, expected)

def test_cyclic_permutation(self):
operators = [qutip.sigmax(), qutip.sigmaz()]
test = gates.expand_operator(qutip.tensor(*operators), N=3,
Expand Down

0 comments on commit 5642874

Please sign in to comment.