Skip to content

Commit

Permalink
[Refact] Factor 2 for Hamevo spectral gap in GPSR (#526)
Browse files Browse the repository at this point in the history
Co-authored-by: Roland-djee <[email protected]>
  • Loading branch information
chMoussa and RolandMacDoland authored Aug 9, 2024
1 parent 6815095 commit 13f3de1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion qadence/blocks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,23 @@ def expression_to_uuids(block: AbstractBlock) -> dict[Expr, list[str]]:
return expr_to_uuid


def uuid_to_eigen(block: AbstractBlock) -> dict[str, Tensor]:
def uuid_to_eigen(
block: AbstractBlock, rescale_eigenvals_timeevo: bool = False
) -> dict[str, Tensor]:
"""Creates a mapping between a parametric block's param_id and its' eigenvalues.
This method is needed for constructing the PSR rules for a given block.
Args:
block (AbstractBlock): Block input
rescale_eigenvals_timeevo (bool, optional): If True, rescale
eigenvalues by 2 for the TimeEvolutionBlock case to allow
differientiating with Hamevo.
Defaults to False.
Returns:
dict[str, Tensor]: Mapping between block's param_id and eigenvalues.
!!! warn
Will ignore eigenvalues of AnalogBlocks that are not yet computed.
"""
Expand All @@ -278,6 +290,11 @@ def uuid_to_eigen(block: AbstractBlock) -> dict[str, Tensor]:
if b.eigenvalues_generator.numel() > 0:
result[uuid] = b.eigenvalues_generator

# GPSR assumes a factor 0.5 for differentiation
# so need rescaling
if isinstance(b, TimeEvolutionBlock) and rescale_eigenvals_timeevo:
result[uuid] *= 2.0

# leave only angle parameter uuid with eigenvals for ConstantAnalogRotation block
if isinstance(block, ConstantAnalogRotation):
break
Expand Down
2 changes: 1 addition & 1 deletion qadence/engines/torch/differentiable_expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def construct_rules(
) -> dict[str, Callable]:
"""Create a mapping between parameters and PSR functions."""

uuid_to_eigs = uuid_to_eigen(circuit.block)
uuid_to_eigs = uuid_to_eigen(circuit.block, rescale_eigenvals_timeevo=True)
# We currently rely on implicit ordering to match the PSR to the parameter,
# because we want to cache PSRs.

Expand Down
2 changes: 1 addition & 1 deletion tests/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

JS_ACCEPTANCE = 7.5e-2
PSR_ACCEPTANCE = 1e-5
GPSR_ACCEPTANCE = 1e-1
GPSR_ACCEPTANCE = 1e-2
ADJOINT_ACCEPTANCE = ATOL_E6
PULSER_GPSR_ACCEPTANCE = 6.0e-2
ATOL_DICT = {
Expand Down

0 comments on commit 13f3de1

Please sign in to comment.