Skip to content

Commit

Permalink
Fix bugs in the analytical pulse shapes (#169)
Browse files Browse the repository at this point in the history
* Fix bugs in the analytical pulse shapes

- Fix bugs in the analytical pulse shapes
- Compress the test. The test no longer depends on the processor class. Test both the maximum and the total area.
  • Loading branch information
BoxiLi committed Sep 11, 2022
1 parent d6e4d52 commit 733b405
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/qutip_qip/compiler/gatecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ def generate_pulse_shape(cls, shape, num_samples, maximum=1.0, area=1.0):
# subjects more to the finite sampling error under interpolation.
# More analytical shape can be added here.
_analytical_window = {
"hann": lambda t: 1 / 2 - 1 / 2 * np.cos(2 * np.pi * t),
"hamming": lambda t: 0.54 - 0.46 * np.cos(2 * np.pi * t),
"hann": lambda t: 1 / 2 - 1 / 2 * np.cos(np.pi * t),
"hamming": lambda t: 0.54 - 0.46 * np.cos(np.pi * t * 2 * 0.54),
}


Expand All @@ -455,7 +455,7 @@ def _normalized_window(shape, num_samples):
raise ValueError(f"Window function {shape} is not supported.")
tlist = np.linspace(0, t_max, num_samples)
if shape in _analytical_window:
coeff = _analytical_window["hann"](tlist)
coeff = _analytical_window[shape](tlist)
else:
coeff = signal.windows.get_window(shape, num_samples)
return coeff, tlist
28 changes: 9 additions & 19 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import numpy as np
from numpy.testing import assert_array_equal
from scipy import integrate
from qutip_qip.compiler.gatecompiler import _default_window_t_max

from qutip_qip.device import (
Expand Down Expand Up @@ -201,22 +202,11 @@ def test_compiler_result_format():


@pytest.mark.parametrize(
"shape", list(_default_window_t_max.keys()) + ["rectangular"])
def test_pulse_shape_scipy(shape):
"""Test different pulse shape functions imported from scipy"""
num_qubits = 1
circuit = QubitCircuit(num_qubits)
circuit.add_gate("X", 0)
processor = LinearSpinChain(num_qubits)
compiler = SpinChainCompiler(num_qubits, processor.params)
compiler.args.update({"shape": shape, "num_samples": 100})
processor.load_circuit(circuit, compiler=compiler)
if shape == "rectangular":
processor.pulse_mode = "discrete"
else:
processor.pulse_mode = "continuous"
init_state = basis(2, 0)
num_result = processor.run_state(init_state).states[-1]
ideal_result = circuit.run(init_state)
ifid = 1 - fidelity(num_result, ideal_result)
assert(pytest.approx(ifid, abs=0.01) == 0)
"shape", list(_default_window_t_max.keys()))
def test_pulse_shape(shape):
"""Test different pulse shape functions"""
coeff, tlist = GateCompiler.generate_pulse_shape(
shape, 1001, maximum=1.0, area=1.0)
assert pytest.approx(coeff[500], 1.e-2) == 1 # max
result = integrate.trapz(coeff, tlist)
assert pytest.approx(result, rel=1.e-2) == 1 # area

0 comments on commit 733b405

Please sign in to comment.