Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions SQDMetal/Utilities/QubitDesigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def func(x):
return abs(self._chi_hertz(x[mps['C_g']], x[mps['C_J']], x[mps['fQubit']], fres, Cres, Lres) / x[mps['chi']] - 1) + abs(self.EJonEC(self._eff_CJ(x[mps['C_J']],x[mps['C_g']], Cres), x[mps['fQubit']]) / x[mps['Ej/Ec']] - 1)

sol = scipy.optimize.minimize(func, x0, bounds=constrs, method='TNC', tol=1e-5)
x = sol.x
x = sol.x.copy()


g = self._g_hertz(x[mps['C_g']], x[mps['C_J']], x[mps['fQubit']], Cres, Lres)
Expand All @@ -252,7 +252,7 @@ def func(x):

sigfigs = 5

print(f"Cost Function Error: {func(x)/2*100}%")
print(f"Cost Function Error: {sol.fun/2*100}%")
self.resonator.print()
print("Qubit:")
print(f"\tFrequency: {GenUtilities.add_units(sol.x[0],sigfigs)}Hz")
Expand Down Expand Up @@ -309,7 +309,7 @@ def func(x):
x0[mps['C_sigma']] = self._Csigma(*x0[1:6])
x0[mps['beta']] = self._beta(*x0[1:6])
sol = scipy.optimize.minimize(func, x0, bounds=constrs, method='Nelder-Mead')
x = sol.x
x = sol.x.copy()

g = self._g_hertz(self._eff_Cg(*sol.x[1:5]), self._eff_Cq(*sol.x[1:5])+sol.x[5], sol.x[0], Cres, Lres)
chi = self._chi_hertz(self._eff_Cg(*sol.x[1:5]), self._eff_Cq(*sol.x[1:5])+sol.x[5], sol.x[0], fres, Cres, Lres)
Expand All @@ -320,7 +320,7 @@ def func(x):
x[mps['Ej/Ec']] = self.EJonEC(self._eff_CJ(self._eff_Cq(*x[1:5])+x[5], self._eff_Cg(*x[1:5]), Cres), x[mps['fQubit']])

sigfigs = 5
cost_err = func(sol.x)/2*100
cost_err = sol.fun/2*100
if print_results:
print(f"Cost Function Error: {cost_err}%")
self.resonator.print()
Expand All @@ -340,7 +340,7 @@ def func(x):
print(f"\tbeta: {GenUtilities.add_units(x[mps['beta']],sigfigs, True)}" + self.chk_constr_resp('beta', x,constrs,mps))

results = {
"cost_error_%": {cost_err},
"cost_error_%": cost_err,
"fQubit_Hz": x[mps["fQubit"]],
"anh_Hz": anh,
"g_Hz": g,
Expand Down
91 changes: 91 additions & 0 deletions UnitTests/test_qubit_optimisation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import contextlib
import io
import json
import re
import unittest

import numpy as np

from SQDMetal.Utilities.QubitDesigner import (
FloatingTransmonDesigner,
ResonatorQuarterWave,
XmonDesigner,
)


class TestQubitOptimisation(unittest.TestCase):
def test_fixed_xmon_reports_original_constraint_residuals(self):
resonator = ResonatorQuarterWave(7e9)
designer = XmonDesigner(resonator)
params = {"fQubit": 4e9, "C_g": 5e-15, "C_J": 60e-15, "chi": -2e6, "Ej/Ec": 50}
cr = resonator.get_res_capacitance()
chi = designer._chi_hertz(
params["C_g"],
params["C_J"],
params["fQubit"],
resonator.get_res_frequency(),
cr,
resonator.get_res_inductance(),
)
ratio = designer.EJonEC(
designer._eff_CJ(params["C_J"], params["C_g"], cr), params["fQubit"]
)
expected = 50 * (
abs(chi / params["chi"] - 1) + abs(ratio / params["Ej/Ec"] - 1)
)

output = io.StringIO()
with contextlib.redirect_stdout(output):
designer.optimise(params)
# Check the numerical objective, not the report's wording.
reported = float(
re.search(
r"[-+]?\d+(?:\.\d*)?(?:e[-+]?\d+)?",
output.getvalue().splitlines()[0],
re.IGNORECASE,
).group()
)
self.assertAlmostEqual(reported, expected, places=10)
self.assertGreater(expected, 1)

def test_floating_result_preserves_target_cost_and_serializes(self):
designer = FloatingTransmonDesigner(ResonatorQuarterWave(7e9))
params = {
"fQubit": 4e9,
"C_q1": 30e-15,
"C_q2": 35e-15,
"C_g1": 5e-15,
"C_g2": 1e-15,
"C_J": 40e-15,
"chi": -2e6,
"Ej/Ec": 50,
"C_sigma": 100e-15,
"beta": 0.1,
}
result = designer.optimise(params, print_results=False)
residuals = [
("chi_Hz", "chi"),
("Ej_Ec_ratio", "Ej/Ec"),
("C_sigma_F", "C_sigma"),
("beta", "beta"),
]
expected = 50 * sum(
abs(result[field] / params[target] - 1) for field, target in residuals
)
self.assertTrue(np.isfinite(expected))
self.assertAlmostEqual(result["cost_error_%"], expected, places=10)
decoded = json.loads(json.dumps(result))
self.assertAlmostEqual(decoded["cost_error_%"], expected, places=10)
for field, target in [
("fQubit_Hz", "fQubit"),
("C_q1_F", "C_q1"),
("C_q2_F", "C_q2"),
("C_g1_F", "C_g1"),
("C_g2_F", "C_g2"),
("C_J_F", "C_J"),
]:
self.assertEqual(result[field], params[target])


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions doc-sphinx/source/general/qbd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ The qubit designer classes all implement an ``optimise`` function:

The function prints a list of the qubit parameters (e.g. :math:`g`, :math:`\chi`, anharmonicity etc.).

The reported cost evaluates the original optimisation targets, not the derived values substituted into the human-readable report. Fully fixed designs are supported even when the optimiser returns a read-only solution array. ``FloatingTransmonDesigner.optimise`` also returns a dictionary of numeric results; ``cost_error_%`` is a scalar percentage, and ``print_results=False`` disables its report without changing those results.

XmonDesigner
~~~~~~~~~~~~

Expand Down