Skip to content
Draft
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
3 changes: 3 additions & 0 deletions src/qrules/argument_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
GraphElementRule,
)
from qrules.quantum_numbers import (
_UNKNOWN,
EdgeQuantumNumber,
NodeQuantumNumber,
Parity,
Expand Down Expand Up @@ -150,6 +151,8 @@ def __extract(
return None
supertype = getattr(self.__obj_type, "__supertype__", None)
if supertype == Parity:
if value is _UNKNOWN: # value of an unknown parity in a property map
return Parity.UNKNOWN
return Parity(int(value)) # ty: ignore[invalid-return-type]
return self.__obj_type(value)

Expand Down
74 changes: 41 additions & 33 deletions src/qrules/conservation_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"""

import operator
from collections.abc import Callable
from collections.abc import Callable, Iterable
from copy import deepcopy
from fractions import Fraction
from functools import reduce
Expand Down Expand Up @@ -215,16 +215,29 @@ def parity_conservation(
outgoing_edge_qns: list[EdgeQN.parity],
l_magnitude: NodeQN.l_magnitude,
) -> bool:
r"""Implement :math:`P_{in} = P_{out} \cdot (-1)^L`."""
if any(p is None for p in [*ingoing_edge_qns, *outgoing_edge_qns]):
return False
r"""Implement :math:`P_{in} = P_{out} \cdot (-1)^L`.

The rule cannot be checked if any of the parities is unknown and then passes.
"""
if not _all_parities_known([*ingoing_edge_qns, *outgoing_edge_qns]):
return True
if len(ingoing_edge_qns) == 1 and len(outgoing_edge_qns) == 2:
parity_in = reduce(lambda x, y: x * y.value, ingoing_edge_qns, 1)
parity_out = reduce(lambda x, y: x * y.value, outgoing_edge_qns, 1)
parity_in = reduce(lambda x, y: x * int(y), ingoing_edge_qns, 1)
parity_out = reduce(lambda x, y: x * int(y), outgoing_edge_qns, 1)
return parity_in == (parity_out * (-1) ** l_magnitude)
return True


def _all_parities_known(parities: Iterable[Parity | None]) -> bool:
return all(p is not None and not p.is_unknown for p in parities)


def _known_parity_value(parity: Parity | None) -> int | None:
if parity is None or parity.is_unknown:
return None
return int(parity)


@frozen
class HelicityParityEdgeInput:
parity: EdgeParity = field(converter=to_parity)
Expand Down Expand Up @@ -254,14 +267,15 @@ def parity_conservation_helicity(

Only the special case :math:`\lambda_1=\lambda_2=0` may return `False`
independent on the parity prefactor.

The rule cannot be checked if any of the parities is unknown and then passes.
"""
if len(ingoing_edge_qns) == 1 and len(outgoing_edge_qns) == 2:
edge_qns = ingoing_edge_qns + outgoing_edge_qns
if not _all_parities_known(x.parity for x in edge_qns):
return True
out_spins = [x.spin_magnitude for x in outgoing_edge_qns]
parity_product = reduce(
lambda x, y: x * y.parity.value if y.parity else x,
ingoing_edge_qns + outgoing_edge_qns,
1,
)
parity_product = reduce(lambda x, y: x * int(y.parity), edge_qns, 1)

prefactor = parity_product * (-1.0) ** (
sum(out_spins) - ingoing_edge_qns[0].spin_magnitude
Expand Down Expand Up @@ -297,15 +311,17 @@ def c_parity_conservation(
) -> bool:
"""Check for :math:`C`-parity conservation.

Implements :math:`C_{in} = C_{out}`.
Implements :math:`C_{in} = C_{out}`. An unknown :math:`C`-parity is treated like an
undefined one: the rule passes if the :math:`C`-parity of a side cannot be
determined.
"""

def _get_c_parity_multiparticle(
part_qns: list[CParityEdgeInput], interaction_qns: CParityNodeInput
) -> int | None:
c_parities_part = [x.c_parity.value for x in part_qns if x.c_parity]
# if all states have C parity defined, then just multiply them
if len(c_parities_part) == len(part_qns):
c_parities_part = [_known_parity_value(x.c_parity) for x in part_qns]
# if all states have C parity defined and known, then just multiply them
if all(c is not None for c in c_parities_part):
return reduce(operator.mul, c_parities_part, 1)

# two particle case
Expand Down Expand Up @@ -354,7 +370,9 @@ def g_parity_conservation( # ruff: ignore[complex-structure]
) -> bool:
"""Check for :math:`G`-parity conservation.

Implements for :math:`G_{in} = G_{out}`.
Implements for :math:`G_{in} = G_{out}`. An unknown :math:`G`-parity is treated like
an undefined one: the rule passes if the :math:`G`-parity of a side cannot be
determined.
"""

def check_multistate_g_parity(
Expand Down Expand Up @@ -382,28 +400,18 @@ def check_g_parity_isobar(
single_state.isospin_magnitude,
(couple_state[0], couple_state[1]),
)
single_state_g_parity = (
ingoing_edge_qns[0].g_parity.value if ingoing_edge_qns[0].g_parity else None
)
single_state_g_parity = _known_parity_value(ingoing_edge_qns[0].g_parity)

if not couple_state_g_parity or not single_state_g_parity:
return True
return couple_state_g_parity == single_state_g_parity

no_g_parity_in_part = [True for x in ingoing_edge_qns if x.g_parity is None]
no_g_parity_out_part = [True for x in outgoing_edge_qns if x.g_parity is None]
# if all states have G parity defined, then just multiply them
if not any(no_g_parity_in_part + no_g_parity_out_part):
in_g_parity = reduce(
lambda x, y: x * y.g_parity.value if y.g_parity else x,
ingoing_edge_qns,
1,
)
out_g_parity = reduce(
lambda x, y: x * y.g_parity.value if y.g_parity else x,
outgoing_edge_qns,
1,
)
# if all states have G parity defined and known, then just multiply them
in_g_parities = [_known_parity_value(x.g_parity) for x in ingoing_edge_qns]
out_g_parities = [_known_parity_value(x.g_parity) for x in outgoing_edge_qns]
if all(g is not None for g in in_g_parities + out_g_parities):
in_g_parity = reduce(operator.mul, in_g_parities, 1)
out_g_parity = reduce(operator.mul, out_g_parities, 1)
return in_g_parity == out_g_parity

# two particle case
Expand Down
13 changes: 12 additions & 1 deletion src/qrules/io/_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from qrules.topology import Edge, FrozenTransition, Topology
from qrules.transition import ReactionInfo, State

_UNKNOWN_PARITY = "?"
"""Serialized value of `.Parity.UNKNOWN`."""


def from_particle_collection(particles: ParticleCollection) -> dict:
"""Serialize a `.ParticleCollection`, sorted by particle name.
Expand Down Expand Up @@ -49,6 +52,8 @@ def _value_serializer(inst: type, field: attrs.Attribute, value: Any) -> Any: #
if isinstance(value, Particle):
return value.name
if isinstance(value, Parity):
if value.is_unknown:
return {"value": _UNKNOWN_PARITY}
return {"value": value.value}
if isinstance(value, Spin):
return {
Expand All @@ -68,14 +73,20 @@ def build_particle_collection(
return ParticleCollection(build_particle(p) for p in definition["particles"])


def _build_parity(definition: dict) -> Parity:
if definition["value"] == _UNKNOWN_PARITY:
return Parity.UNKNOWN
return Parity(**definition)


def build_particle(definition: dict) -> Particle:
isospin_def = definition.get("isospin")
if isospin_def is not None:
definition["isospin"] = Spin(**isospin_def)
for parity in ["parity", "c_parity", "g_parity"]:
parity_def = definition.get(parity)
if parity_def is not None:
definition[parity] = Parity(**parity_def)
definition[parity] = _build_parity(parity_def)
return Particle(**definition)


Expand Down
9 changes: 8 additions & 1 deletion src/qrules/io/_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import attrs

from qrules.particle import Particle, ParticleWithSpin, Spin, _render_fraction
from qrules.quantum_numbers import InteractionProperties
from qrules.quantum_numbers import InteractionProperties, _Unknown
from qrules.solving import (
EdgeSettings,
GraphEdgePropertyMap,
Expand Down Expand Up @@ -319,6 +319,13 @@ def _(_: None) -> str:
return R"\mathrm{None}"


@as_latex.register(_Unknown)
@as_string.register(_Unknown)
def _(_: _Unknown) -> str:
"""Render the value of an unknown `.Parity`, for instance in edge properties."""
return "?"


@as_string.register(dict)
def _(obj: dict) -> str:
return __render_mapping(obj, _PLAIN_FORMATTER)
Expand Down
2 changes: 1 addition & 1 deletion src/qrules/particle-validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "object",
"required": ["value"],
"properties": {
"value": { "enum": [-1, 1] },
"value": { "enum": [-1, 1, "?"] },
"additionalProperties": false
}
},
Expand Down
4 changes: 3 additions & 1 deletion src/qrules/particle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ def _repr_pretty_(self, p: RepresentationPrinter, cycle: bool) -> None:
if value != attribute.default:
p.breakable()
p.text(f"{attribute.name}=")
if isinstance(value, Parity):
if isinstance(value, Parity) and value.is_unknown:
p.text(repr(value))
elif isinstance(value, Parity):
p.text(_float_as_signed_str(int(value), render_plus=True))
else:
p.pretty(value)
Expand Down
58 changes: 58 additions & 0 deletions src/qrules/particle/_adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Shared helpers for converting external particle databases to QRules particles."""

from __future__ import annotations

from typing import TYPE_CHECKING

import attrs

from qrules.quantum_numbers import Parity

if TYPE_CHECKING:
from qrules.particle import Particle


def mark_unknown_parities(particle: Particle) -> Particle:
"""Distinguish parities that are not defined from parities that are unknown.

Particle databases do not consistently distinguish a parity that does not exist for
a particle from a parity that exists, but has not been measured. This function
decides that based on the other quantum numbers of the particle:

- The (spatial) parity exists for all particles, so a missing parity is unknown.
- :math:`C`-parity exists only for particles that are their own antiparticle.
- :math:`G`-parity exists only for non-strange, flavorless mesons with isospin.

Missing parities that exist are set to `.Parity.UNKNOWN`, the others stay `None`.
"""
changes: dict[str, Parity] = {}
if particle.parity is None:
changes["parity"] = Parity.UNKNOWN
if particle.c_parity is None and _has_c_parity(particle):
changes["c_parity"] = Parity.UNKNOWN
if particle.g_parity is None and _has_g_parity(particle):
changes["g_parity"] = Parity.UNKNOWN
if not changes:
return particle
return attrs.evolve(particle, **changes)


def _has_c_parity(particle: Particle) -> bool:
return particle.charge == 0 and _is_flavorless_boson(particle)


def _has_g_parity(particle: Particle) -> bool:
return particle.isospin is not None and _is_flavorless_boson(particle)


def _is_flavorless_boson(particle: Particle) -> bool:
return not any((
particle.baryon_number,
particle.strangeness,
particle.charmness,
particle.bottomness,
particle.topness,
particle.electron_lepton_number,
particle.muon_lepton_number,
particle.tau_lepton_number,
))
44 changes: 25 additions & 19 deletions src/qrules/particle/_adapter/pdg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pdg.units import convert

from qrules.particle import Particle, ParticleCollection, Spin
from qrules.particle._adapter import mark_unknown_parities
from qrules.quantum_numbers import Parity

if TYPE_CHECKING:
Expand Down Expand Up @@ -107,25 +108,28 @@ def _convert_particle(source: PdgParticle) -> Particle:
parity = Parity(+1 if mcid > 0 else -1)
c_parity = _to_parity(source.quantum_C) if source.self_conjugate else None

return Particle(
name=source.name,
pid=mcid,
spin=spin,
mass=mass,
width=width,
charge=charge,
isospin=isospin,
strangeness=strangeness,
charmness=charmness,
bottomness=bottomness,
topness=topness,
baryon_number=baryon_number,
electron_lepton_number=lepton_numbers[0],
muon_lepton_number=lepton_numbers[1],
tau_lepton_number=lepton_numbers[2],
parity=parity,
c_parity=c_parity,
g_parity=_to_parity(source.quantum_G),
# The PDG API leaves out parities that are undefined or not listed (like for W, Z)
return mark_unknown_parities(
Particle(
name=source.name,
pid=mcid,
spin=spin,
mass=mass,
width=width,
charge=charge,
isospin=isospin,
strangeness=strangeness,
charmness=charmness,
bottomness=bottomness,
topness=topness,
baryon_number=baryon_number,
electron_lepton_number=lepton_numbers[0],
muon_lepton_number=lepton_numbers[1],
tau_lepton_number=lepton_numbers[2],
parity=parity,
c_parity=c_parity,
g_parity=_to_parity(source.quantum_G),
)
)


Expand Down Expand Up @@ -233,6 +237,8 @@ def _to_parity(value: str | None) -> Parity | None:
return Parity(+1)
if value == "-":
return Parity(-1)
if value == "?":
return Parity.UNKNOWN
return None


Expand Down
5 changes: 4 additions & 1 deletion src/qrules/particle/_adapter/scikit_hep.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from particle import Particle as PdgDatabase

from qrules.particle import Particle, ParticleCollection, Spin
from qrules.particle._adapter import mark_unknown_parities
from qrules.quantum_numbers import Parity

if TYPE_CHECKING:
Expand Down Expand Up @@ -73,7 +74,7 @@ def convert_spin(value: Fraction | float | None) -> float:
latex = None
if pdg_particle.latex_name != "Unknown":
latex = str(pdg_particle.latex_name)
return Particle(
particle = Particle(
name=str(pdg_particle.name),
latex=latex,
pid=int(pdg_particle.pdgid),
Expand All @@ -94,6 +95,8 @@ def convert_spin(value: Fraction | float | None) -> float:
c_parity=__create_parity(pdg_particle.C),
g_parity=__create_parity(pdg_particle.G),
)
# Scikit-HEP uses Parity.u for both undefined and unknown parities
return mark_unknown_parities(particle)


def __compute_quark_numbers(
Expand Down
Loading
Loading