Skip to content

Commit

Permalink
Hadamard transform efficient (#103)
Browse files Browse the repository at this point in the history
Added tests for Hadamard and improved its construction efficiency.
  • Loading branch information
AGaliciaMartinez committed Oct 14, 2021
1 parent 73411ae commit bd85402
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 45 deletions.
76 changes: 38 additions & 38 deletions doc/source/qip-simulator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ method.

.. testcode::

from qutip import tensor
from qutip import tensor, basis
zero_state = tensor(basis(2, 0), basis(2, 0), basis(2, 0))
result = qc.run(state=zero_state)
wstate = result
Expand Down Expand Up @@ -94,42 +94,42 @@ outputs, we can use the :meth:`.QubitCircuit.run_statistics` function:
.. testoutput::
:options: +NORMALIZE_WHITESPACE

State:
Quantum object: dims = [[2, 2, 2], [1, 1, 1]], shape = (8, 1), type = ket
Qobj data =
[[0.]
[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[0.]]
with probability 0.33333257054168813
State:
Quantum object: dims = [[2, 2, 2], [1, 1, 1]], shape = (8, 1), type = ket
Qobj data =
[[0.]
[0.]
[1.]
[0.]
[0.]
[0.]
[0.]
[0.]]
with probability 0.33333257054168813
State:
Quantum object: dims = [[2, 2, 2], [1, 1, 1]], shape = (8, 1), type = ket
Qobj data =
[[0.]
[0.]
[0.]
[0.]
[1.]
[0.]
[0.]
[0.]]
with probability 0.33333485891662384
State:
Quantum object: dims = [[2, 2, 2], [1, 1, 1]], shape = (8, 1), type = ket
Qobj data =
[[0.]
[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[0.]]
with probability 0.3333325705416881
State:
Quantum object: dims = [[2, 2, 2], [1, 1, 1]], shape = (8, 1), type = ket
Qobj data =
[[0.]
[0.]
[1.]
[0.]
[0.]
[0.]
[0.]
[0.]]
with probability 0.3333325705416881
State:
Quantum object: dims = [[2, 2, 2], [1, 1, 1]], shape = (8, 1), type = ket
Qobj data =
[[0.]
[0.]
[0.]
[0.]
[1.]
[0.]
[0.]
[0.]]
with probability 0.33333485891662384

The function returns a :class:`~.Result` object which contains
the output states.
Expand Down Expand Up @@ -187,7 +187,7 @@ The :class:`.CircuitSimulator` class also enables stepping through the circuit:
[0. ]
[0. ]]

This only excutes one gate in the circuit and
This only executes one gate in the circuit and
allows for a better understanding of how the state evolution takes place.
The method steps through both the gates and the measurements.

Expand Down
10 changes: 3 additions & 7 deletions src/qutip_qip/operations/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,14 +1387,10 @@ 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)
]
)
data = [[1, 1], [1, -1]]
H = Qobj(data) / np.sqrt(2)

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


def _flatten(lst):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,23 @@ 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 bd85402

Please sign in to comment.