diff --git a/tensorcircuit/templates/blocks.py b/tensorcircuit/templates/blocks.py index 1e376903..b4983dc6 100644 --- a/tensorcircuit/templates/blocks.py +++ b/tensorcircuit/templates/blocks.py @@ -181,10 +181,10 @@ def qft( for i in range(len(index) // 2): c.swap(index[i], index[len(index) - 1 - i]) for i in range(len(index) - 1, -1, -1): - rotation = -np.pi / 2 + rotation = -np.pi / 2 ** (len(index) - i - 1) for j in range(len(index) - 1, i, -1): c.cphase(index[j], index[i], theta=rotation) - rotation /= 2 + rotation *= 2 c.H(index[i]) if insert_barriers: c.barrier_instruction(range(min(index), max(index) + 1)) diff --git a/tests/test_templates.py b/tests/test_templates.py index 79e2b176..219f64f0 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -66,10 +66,20 @@ def test_bell_block(): def test_qft_block() -> None: - c = tc.Circuit(4) - c = tc.templates.blocks.qft(c, 0, 1, 2, 3) - s = c.perfect_sampling() - assert s[1] - 0.0624999 < 10e-6 + n_qubits = 4 + c = tc.Circuit(n_qubits) + c = tc.templates.blocks.qft(c, *range(n_qubits)) + mat = c.quoperator().eval().reshape(2 ** (n_qubits), -1) + N = 2**n_qubits + ref = np.exp( + 1j * 2 * np.pi * np.arange(N).reshape(-1, 1) * np.arange(N).reshape(1, -1) / N + ) / np.sqrt(N) + np.testing.assert_allclose(mat, ref, atol=1e-7) + + c = tc.Circuit(n_qubits) + c = tc.templates.blocks.qft(c, *range(n_qubits), inverse=True) + mat = c.quoperator().eval().reshape(2 ** (n_qubits), -1) + np.testing.assert_allclose(mat, ref.T.conj(), atol=1e-7) def test_grid_coord():