-
Notifications
You must be signed in to change notification settings - Fork 7
UNITARY HACK--Fix dtype exceptions and add test for dtype conversion #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| import jax | ||
| import jax.numpy as jnp | ||
| import functools | ||
| import numpy as np | ||
| import warnings | ||
| import re | ||
|
|
||
| from qurveros.settings import settings | ||
|
|
@@ -90,35 +92,6 @@ def __init__(self, *, curve, order, interval, params=None, | |
|
|
||
| # Ensure correct types for frenet_dict calculations. | ||
| if curve is not None: | ||
| if isinstance(curve, str): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole deleted block from line 93 to line 121 should not have been deleted. When you look at the diff between the main branch and your code, you should see only the two insertions in lines 9 and 10, and then the insertions for the type conversion. |
||
| #Ensure characters are safe | ||
| if re.search(r'[^0-9A-Za-z_ \[\],\+\-\*\/\(\).]', curve): | ||
| raise ValueError("Unsafe characters in curve expression") | ||
|
|
||
| #Get params | ||
| raw_names = [m.group(1) | ||
| for m in re.finditer(r'\b([A-Za-z_]\w*)\b', curve)] | ||
| #Find all of the math functions within jnp | ||
| safe_math = { | ||
| name: getattr(jnp, name) | ||
| for name in dir(jnp) | ||
| if not name.startswith("_") | ||
| } | ||
| reserved = set(safe_math) | {'x', 'jnp'} | ||
| #Preserve first-seen order | ||
| param_names = [n for n in dict.fromkeys(raw_names) if n not in reserved] | ||
|
|
||
| #Build the source | ||
| src = "def _f(x, params):\n" | ||
| if param_names: | ||
| src += f" {', '.join(param_names)} = params\n" | ||
| src += f" return jnp.array({curve})" | ||
|
|
||
| #Create the exec | ||
| safe_globals = {"__builtins__": None, "jnp": jnp, **safe_math} | ||
| exec(src, safe_globals) | ||
| curve = safe_globals['_f'] | ||
|
|
||
| def curve_fun(x, params): | ||
| return 1.0*jnp.array(curve(x, params)).flatten() | ||
|
|
||
|
|
@@ -163,7 +136,29 @@ def set_params(self, params): | |
| evaluated for the new set of auxiliary parameters. | ||
| """ | ||
|
|
||
| self.params = params | ||
| #Determine the correct float type based on JAX config | ||
| float_dtype= jnp.float64 if jax.config.jax_enable_x64 else jnp.float32 | ||
|
|
||
| def convert_value(v): | ||
|
|
||
| """Helper function that recursively convert values to JAX-compatible floats""" | ||
| if isinstance(v,(jnp.ndarray,np.ndarray)): | ||
| if not jnp.issubdtype(v.dtype,jnp.floating): | ||
| warnings.warn(f"Converting array to {float_dtype}") | ||
| return jnp.asarray(v,dtype=float_dtype) | ||
| return v | ||
| elif isinstance(v,(list,tuple)): | ||
| return jnp.asarray(v,dtype=float_dtype) | ||
| elif isinstance(v,(int,float)): | ||
| warnings.warn(f"Converting scalar{v} to {float_dtype}") | ||
| return jnp.array(v, dtype=float_dtype) | ||
| return v | ||
|
|
||
| #Convert all parameters while preserving the original structure | ||
| converted_params ={ | ||
| k:convert_value(v) for k ,v in params.items() | ||
| } | ||
| self.params = converted_params | ||
|
|
||
| self.frenet_dict = None | ||
| self.control_dict = None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import unittest | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think something went wrong with this whole file, because now all of the tests you had previously have been deleted. I would recommend reverting to the version of this file from the previous commit and then turning that into a unit test to align with the other test files. |
||
| import jax.numpy as jnp | ||
| import numpy as np | ||
| import qurveros.barqtools as barqtools | ||
|
|
||
| init_pgf = barqtools.get_default_pgf_params_dict() | ||
| init_pgf['norm_value'] = 0.25 | ||
|
|
||
| import jax.numpy as jnp | ||
| init_free_points = jnp.array([ | ||
| [0, 1, 1], | ||
| [0, 1, 2], | ||
| [1, 1, 2], | ||
| [2, 1, 2], | ||
| [2, 0, 2], | ||
| [2, -1, 2], | ||
| [2, -1, 1], | ||
| [2, -1, 0], | ||
| [1, -1, 0], | ||
| [0, -1, 0] | ||
| ]) #10 | ||
|
|
||
| # init_free_points = jnp.array([ | ||
| # [0.0, 0.0, 0.0], | ||
| # [-0.9174368396882462, 0.20027364852868498, 0.21369406409562192], | ||
| # [-0.8041761519991039, 0.17554918775154812, 0.1873128075256522], | ||
| # [1.0143787013738685, 0.17178532468454982, 0.2736775539730863], | ||
| # [0.38083479899988426, 0.5492027916657358, -0.0006194580232775966], | ||
| # [-0.5354359751936735, 0.2523799047994231, 0.17442024487353], | ||
| # [-0.9418994108358996, 0.3539711361917974, -0.232999217817437], | ||
| # [-0.5711036813837912, -0.05713223886502761, 0.0018145161138630609], | ||
| # [-0.1905484142468627, 0.14036688528159752, -0.04564339757313231], | ||
| # [0.21930072573262832, -0.688227292792582, -0.004032082528444142], | ||
| # [0.3428011852341114, -0.4263672394739059, -0.4215955544900982], | ||
| # [0.13930670327019212, -0.43162490061771347, -0.18005032077844998], | ||
| # [0.2537454430731247, -0.07892925578860352, -0.3437089732582968], | ||
| # [0.6188165455173736, 0.2788967540319768, -0.1954681209080432], | ||
| # [0.23319152157880507, 0.22028400307332546, 0.007186201125869842], | ||
| # [0.5468775591637357, -0.27531072025575787, 0.40647697079809975], | ||
| # [0.31340505721501555, 0.24480221323268164, 0.24650669307439751], | ||
| # [-0.24996005966023707, 0.313430826389596, 0.9184770058348332], | ||
| # [-0.43575661910587743, -0.7757470303594219, -0.417375597747747], | ||
| # [-0.9816177358001005, 0.21428414132133258, 0.2286434054934467], | ||
| # [-0.9016288172030048, 0.19682280569985486, 0.21001197893831114], | ||
| # [0.0, 0.0, 0.0]]) #22 | ||
| # import qurveros.optspacecurve as barq | ||
| from qurveros.optspacecurve import BarqCurve as barq | ||
| import qutip | ||
| from qurveros.qubit_bench import quantumtools | ||
|
|
||
|
|
||
|
|
||
|
|
||
| u_target = qutip.sigmax() | ||
| adj_target = quantumtools.calculate_adj_rep(u_target) | ||
|
|
||
| barq_instance = barq(adj_target=adj_target, n_free_points=10) | ||
|
|
||
| barq_instance.initialize_parameters(init_free_points=init_free_points, | ||
| init_pgf_params=init_pgf) | ||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Uh oh!
There was an error while loading. Please reload this page.