Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add threshold to ControlCostArea; bug fix in sum over controls #15

Merged
dkweiss31 merged 12 commits intodkweiss31:mainfrom
HarshBabla99:bug_fixes
Aug 8, 2025
Merged

Add threshold to ControlCostArea; bug fix in sum over controls #15
dkweiss31 merged 12 commits intodkweiss31:mainfrom
HarshBabla99:bug_fixes

Conversation

@HarshBabla99
Copy link
Contributor

Bug fixes:

  • The control drive area is computed as a Riemann sum; however, the dt factor was missing. Fixed this bug.
  • Fixed a bug caused by dynamiqs #974. The new TimeQArray.prefactor() method in the parent class now adds a prefactor to all children, including ConstantTimeQArray. Previously, qontrol checked for the presence or absence of a prefactor attribute, but now I check if a Hamiltonian is a ConstantTimeQArray.

Optimizations:

  • Avoid computing propagator dim at every call
  • Minimize numpy dependence
  • Remove redundant line in loss()

Feature:

  • Added a threshold for the control area, which is especially useful for bosonic control. When optimizing a pulse on a truncated oscillator, this helps prevent displacements beyond the Hilbert space cutoff.

qontrol/cost.py Outdated
Comment on lines 438 to 439
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you can now call prefactor with an array making the vmap unnecesary, nice! (I don't recall if that was always the case...)

qontrol/cost.py Outdated
Comment on lines 478 to 480
Copy link
Owner

@dkweiss31 dkweiss31 Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we make the same change in ControlCostNorm? Maybe this logic wants to be in the ControlCost _evaluate_at_tsave function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! That makes sense.

import dynamiqs as dq
import jax
import jax.numpy as jnp
import numpy as np
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll want to undo the changes to get rid of numpy. You'll notice that all of the calls to numpy are outside of jitted regions. Calls to jax or jax.numpy outside of jitted regions just slow you down AFAIK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this would be the case on a CPU. Is this also true on a GPU?
I presume numpy commands are always run on the CPU. If we use numpy, the process would be bottlenecked by data transfer back and forth between a GPU and a CPU.
However, I presume jax.numpy could perform all operations on a GPU directly.
I guess we should compare the slowdown due to jax.numpy versus that due to CPU-GPU data transfer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the bottleneck as you noticed will be saving data and updating the plots, both of which must happen on a CPU. So there is no harm in using numpy in that context. Again, if the jax code is not in a jitted region, then no matter where you run the code (CPU, GPU, TPU) this will be slower than pure numpy (since the code doesn't get compiled).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Will undo the np->jnp changes.


import jax
import matplotlib.pyplot as plt
import numpy as np
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file as well, we shouldn't be calling jnp

@dkweiss31 dkweiss31 self-requested a review July 30, 2025 23:14
Copy link
Owner

@dkweiss31 dkweiss31 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far! See my comments: I think the dt logic wants to be in ControlCost, and you'll want to restore most/all calls to numpy since those calls are outside of jit regions

@dkweiss31
Copy link
Owner

Also for all of these PRs: you'll want to run in terminal

task format

and

task lint

The first call autoformats the code and the second call will optimize some imports but also tell you various things you need to fix (lines can't be longer than 88 characters, remove whitespaces, annotate arguments, etc.)

Notice that this is the reason all of the checks are failing

@HarshBabla99
Copy link
Contributor Author

Implemented requested changes.

@dkweiss31 dkweiss31 changed the title Bug fixes and minor features Add threshold to ControlCostArea; bug fix in sum over controls Aug 8, 2025
Copy link
Owner

@dkweiss31 dkweiss31 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @HarshBabla99 !

@dkweiss31 dkweiss31 merged commit d8001b7 into dkweiss31:main Aug 8, 2025
1 check passed
@HarshBabla99 HarshBabla99 deleted the bug_fixes branch August 8, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments