Skip to content

Commit bdd4472

Browse files
committed
fit inverse qft bug
1 parent 625de73 commit bdd4472

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

tensorcircuit/templates/blocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ def qft(
181181
for i in range(len(index) // 2):
182182
c.swap(index[i], index[len(index) - 1 - i])
183183
for i in range(len(index) - 1, -1, -1):
184-
rotation = -np.pi / 2
184+
rotation = -np.pi / 2 ** (len(index) - i - 1)
185185
for j in range(len(index) - 1, i, -1):
186186
c.cphase(index[j], index[i], theta=rotation)
187-
rotation /= 2
187+
rotation *= 2
188188
c.H(index[i])
189189
if insert_barriers:
190190
c.barrier_instruction(range(min(index), max(index) + 1))

tests/test_templates.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,20 @@ def test_bell_block():
6666

6767

6868
def test_qft_block() -> None:
69-
c = tc.Circuit(4)
70-
c = tc.templates.blocks.qft(c, 0, 1, 2, 3)
71-
s = c.perfect_sampling()
72-
assert s[1] - 0.0624999 < 10e-6
69+
n_qubits = 4
70+
c = tc.Circuit(n_qubits)
71+
c = tc.templates.blocks.qft(c, *range(n_qubits))
72+
mat = c.quoperator().eval().reshape(2 ** (n_qubits), -1)
73+
N = 2**n_qubits
74+
ref = np.exp(
75+
1j * 2 * np.pi * np.arange(N).reshape(-1, 1) * np.arange(N).reshape(1, -1) / N
76+
) / np.sqrt(N)
77+
np.testing.assert_allclose(mat, ref, atol=1e-7)
78+
79+
c = tc.Circuit(n_qubits)
80+
c = tc.templates.blocks.qft(c, *range(n_qubits), inverse=True)
81+
mat = c.quoperator().eval().reshape(2 ** (n_qubits), -1)
82+
np.testing.assert_allclose(mat, ref.T.conj(), atol=1e-7)
7383

7484

7585
def test_grid_coord():

0 commit comments

Comments
 (0)