Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hadamard transform efficient #1688

Merged
merged 4 commits into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions qutip/qip/operations/gates.py
Expand Up @@ -892,11 +892,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 @@ -299,6 +299,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