Skip to content
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

FEAT: implement quantum problem set filter #287

Merged
merged 38 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f190b2f
Added '*.swp' (temporary vim-files) to .gitignore
grayson-helmholz Jul 19, 2024
67b6747
Added test-skeleton for Issue#272
grayson-helmholz Jul 23, 2024
631f692
created QNP-setter and its test
grayson-helmholz Aug 6, 2024
c18d411
inlined fixture-arguments, function-renaming
grayson-helmholz Aug 6, 2024
5a997d0
iss272: faulty filters for QNProblemSet
grayson-helmholz Aug 8, 2024
ad5ea05
FIX: stabilize test fixture
redeboer Aug 8, 2024
6fbf83b
added pid&spin_projection -> non-zero results
grayson-helmholz Aug 21, 2024
557ec57
cleanup of test_solving.py
grayson-helmholz Aug 21, 2024
e54289e
changed tuple to set in test-case
grayson-helmholz Aug 22, 2024
b99bcdc
Merge branch 'main' into filter_quantum_numbers
grayson-helmholz Aug 26, 2024
99a594d
got the test_solving.py from old branch: remove_spin-proj[...]
grayson-helmholz Aug 27, 2024
559b2f4
moved Edge/NodeQuantumNumberTypes to quantum_numbers.py
grayson-helmholz Aug 27, 2024
2808ed7
domain-keys now have specific type
grayson-helmholz Aug 27, 2024
75e743c
Merge branch 'main' into filter_quantum_numbers
grayson-helmholz Oct 16, 2024
8332f09
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
a1287e8
Moved import of Iterable into TYPE_CHECKING-Block
grayson-helmholz Oct 16, 2024
ed844c4
added 'blank' qn's in conf.py
grayson-helmholz Oct 16, 2024
f48d0ed
changed qn's from obj to class
grayson-helmholz Oct 16, 2024
d1c0cc8
Merge branch 'main' into filter_quantum_numbers
grayson-helmholz Oct 18, 2024
a58b774
cleanup
grayson-helmholz Oct 18, 2024
da7ac25
Merge branch 'filter_quantum_numbers' of github.com:CompWA/qrules int…
grayson-helmholz Oct 18, 2024
b8cb284
using specific no. of soltuions in test_solving
grayson-helmholz Oct 21, 2024
6f8102d
moved problem-set-filter, added docstring
grayson-helmholz Oct 21, 2024
25c716e
example of filter-function in visualize-notebook
grayson-helmholz Oct 29, 2024
3102359
parametrized filter-test, added Rules and Edge/Node-Types to conf
grayson-helmholz Oct 29, 2024
fddd324
moved Rules and Edge/Node-Types in conf to target_substitutions
grayson-helmholz Oct 29, 2024
1ff13a1
reworked conf, added docstrings to Rules and Edge/NodeTypes
grayson-helmholz Oct 29, 2024
56bb188
Merge branch 'main' into filter_quantum_numbers
grayson-helmholz Oct 29, 2024
c941876
MAINT: sort `api_target_substitutions`
redeboer Oct 30, 2024
1231441
modified docstrings for cross-refs
grayson-helmholz Nov 5, 2024
0bb42cc
renamed arguments, added `dict_set_intersection`
grayson-helmholz Nov 5, 2024
1099cf5
changed type-hint, removed redundant variable
grayson-helmholz Nov 5, 2024
678c4fb
Merge remote-tracking branch 'origin/main' into filter_quantum_numbers
grayson-helmholz Nov 5, 2024
d735bbd
reverted version-changes and hidden-sources in notebooks
grayson-helmholz Nov 5, 2024
4bbcada
fixed bug in #301, new note in warning
grayson-helmholz Nov 5, 2024
1d22412
Merge branch 'filter_quantum_numbers' of github.com:CompWA/qrules int…
grayson-helmholz Nov 5, 2024
a0bef13
test_solving now asserts different no. of solutions
grayson-helmholz Nov 5, 2024
929bc48
fixed hitting assert twice
grayson-helmholz Nov 5, 2024
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 docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def pick_newtype_attrs(some_type: type) -> list:
add_module_names = False
api_github_repo = f"{ORGANIZATION}/{REPO_NAME}"
api_target_substitutions: dict[str, str | tuple[str, str]] = {
"EdgeQuantumNumberTypes": ("obj", "qrules.quantum_numbers.EdgeQuantumNumberTypes"),
"EdgeType": "typing.TypeVar",
"GraphEdgePropertyMap": ("obj", "qrules.argument_handling.GraphEdgePropertyMap"),
"GraphElementProperties": ("obj", "qrules.solving.GraphElementProperties"),
Expand All @@ -56,11 +57,13 @@ def pick_newtype_attrs(some_type: type) -> list:
"NewEdgeType": "typing.TypeVar",
"NewNodeType": "typing.TypeVar",
"NodeQuantumNumber": ("obj", "qrules.quantum_numbers.NodeQuantumNumber"),
"NodeQuantumNumberTypes": ("obj", "qrules.quantum_numbers.NodeQuantumNumberTypes"),
"NodeType": "typing.TypeVar",
"ParticleWithSpin": ("obj", "qrules.particle.ParticleWithSpin"),
"Path": "pathlib.Path",
"qrules.topology.EdgeType": "typing.TypeVar",
"qrules.topology.NodeType": "typing.TypeVar",
"Rule": ("obj", "qrules.argument_handling.Rule"),
"SpinFormalism": ("obj", "qrules.transition.SpinFormalism"),
"StateDefinition": ("obj", "qrules.combinatorics.StateDefinition"),
"StateTransition": ("obj", "qrules.transition.StateTransition"),
Expand Down
5 changes: 1 addition & 4 deletions docs/usage/conservation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
Expand Down Expand Up @@ -417,7 +414,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/reaction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
"version": "3.12.7"
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
}
},
"nbformat": 4,
Expand Down
115 changes: 108 additions & 7 deletions docs/usage/visualize.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
":::{warning}\n",
"Currently the main user-interface is the ```StateTransitionManager```. There is work in progress to remove it and split its functionality into several functions/classes to separate concerns\n",
"and to facilitate the modification of intermediate results like the filtering of ```QNProblemSet```s, setting allowed interaction types, etc. (see below)\n",
":::"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -103,7 +113,14 @@
"from IPython.display import display\n",
"\n",
"import qrules\n",
"from qrules.conservation_rules import (\n",
" parity_conservation,\n",
" spin_magnitude_conservation,\n",
" spin_validity,\n",
")\n",
"from qrules.particle import Spin\n",
"from qrules.quantum_numbers import EdgeQuantumNumbers, NodeQuantumNumbers\n",
"from qrules.solving import CSPSolver, filter_quantum_number_problem_set\n",
"from qrules.topology import create_isobar_topologies, create_n_body_topology\n",
"from qrules.transition import State"
]
Expand Down Expand Up @@ -283,9 +300,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
"full-width",
"hide-input"
Expand All @@ -301,9 +315,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"full-width",
"hide-input"
Expand All @@ -315,6 +326,96 @@
"graphviz.Source(dot)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Filtering Quantum Number Problem Sets\n",
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
"Sometimes only a certain subset of quantum numbers & conservation rules are relevant or the number of solutions the {class}`.StateTransitionManager` gives by default is to large for the follow-up analysis.\n",
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
"The {func}`.filter_quantum_number_problem_set` function can be used to produce a {class}`.QNProblemSet` where only the desired quantum numbers & conservation rules are considered when fed back to the {class}`.StateTransitionManager`."
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"desired_edge_rules = {spin_validity}\n",
"desired_node_rules = {spin_magnitude_conservation, parity_conservation}\n",
"desired_edge_quantum_numbers = {\n",
" EdgeQuantumNumbers.spin_magnitude,\n",
" EdgeQuantumNumbers.parity,\n",
"}\n",
"desired_node_quantum_numbers = {\n",
" NodeQuantumNumbers.l_magnitude,\n",
" NodeQuantumNumbers.s_magnitude,\n",
"}\n",
"\n",
"filtered_qn_problem_set = filter_quantum_number_problem_set(\n",
" qn_problem_set,\n",
" desired_edge_rules,\n",
" desired_node_rules,\n",
" desired_edge_quantum_numbers,\n",
" desired_node_quantum_numbers,\n",
")"
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-cell"
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
]
},
"outputs": [],
"source": [
"dot = qrules.io.asdot(filtered_qn_problem_set, render_node=True)\n",
"graphviz.Source(dot)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
":::{warning}\n",
"The next cell will use some (currently) internal functionality. As statet at the top, a workflow similar to this will be used in future versions of ```qrules```.\n",
":::"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"solver = CSPSolver([\n",
" qrules.system_control.create_edge_properties(part)\n",
" for part in qrules.particle.load_pdg()\n",
"])\n",
"\n",
"filtered_qn_solutions = solver.find_solutions(filtered_qn_problem_set)\n",
"filtered_qn_result = filtered_qn_solutions.solutions[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-cell"
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
]
},
"outputs": [],
"source": [
"dot = qrules.io.asdot(filtered_qn_result, render_node=True)\n",
"graphviz.Source(dot)"
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -672,7 +773,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
1 change: 1 addition & 0 deletions src/qrules/argument_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Scalar = Union[int, float]

Rule = Union[GraphElementRule, EdgeQNConservationRule, ConservationRule]
"""Any type of rule"""

_ElementType = TypeVar("_ElementType")

Expand Down
5 changes: 3 additions & 2 deletions src/qrules/quantum_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class EdgeQuantumNumbers:
edge_qn_type.__module__ = __name__


# for static typing
EdgeQuantumNumber = Union[
EdgeQuantumNumbers.pid,
EdgeQuantumNumbers.mass,
Expand All @@ -126,8 +125,8 @@ class EdgeQuantumNumbers:
EdgeQuantumNumbers.c_parity,
EdgeQuantumNumbers.g_parity,
]
"""Type hint for quantum numbers of edges"""

# for accessing the keys of the dicts in EdgeSettings
EdgeQuantumNumberTypes = Union[
type[EdgeQuantumNumbers.pid],
type[EdgeQuantumNumbers.mass],
Expand All @@ -149,6 +148,7 @@ class EdgeQuantumNumbers:
type[EdgeQuantumNumbers.c_parity],
type[EdgeQuantumNumbers.g_parity],
]
"""Type-Union for accessing the keys of the dicts in EdgeSettings"""

grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved

@frozen(init=False)
Expand Down Expand Up @@ -186,6 +186,7 @@ class NodeQuantumNumbers:
type[NodeQuantumNumbers.s_projection],
type[NodeQuantumNumbers.parity_prefactor],
]
"""Type-Union for accessing the keys of the dicts in NodeSettings"""

grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved

def _to_optional_float(optional_float: float | None) -> float | None:
Expand Down
80 changes: 80 additions & 0 deletions src/qrules/solving.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,86 @@ def topology(self) -> Topology:
return self.initial_facts.topology


def filter_quantum_number_problem_set(
quantum_number_problem_set: QNProblemSet,
edge_rules: set[GraphElementRule],
node_rules: set[Rule],
edge_properties_and_domains: Iterable[EdgeQuantumNumberTypes],
node_properties_and_domains: Iterable[NodeQuantumNumberTypes],
grayson-helmholz marked this conversation as resolved.
Show resolved Hide resolved
) -> QNProblemSet:
"""Filter `QNProblemSet` for desired conservation rules, settings and domains.

Currently it is the responsibility of the user to provide fitting properties
and domains for the correspinding conservation rules.

Args:
quantum_number_problem_set: `QNProblemSet` as generated by `CSPSolver`.
edge_rules: Conservation rules regarding the edges.
node_rules: Conservation rules regarding the nodes.
edge_properties_and_domains: Edge settings, properties and domains.
node_properties_and_domains: Node settings, properties and domains.
"""
old_edge_settings = quantum_number_problem_set.solving_settings.states
old_node_settings = quantum_number_problem_set.solving_settings.interactions
old_edge_properties = quantum_number_problem_set.initial_facts.states
old_node_properties = quantum_number_problem_set.initial_facts.interactions
new_edge_settings = {
edge_id: EdgeSettings(
conservation_rules=edge_rules,
rule_priorities=edge_setting.rule_priorities,
qn_domains=({
key: val
for key, val in edge_setting.qn_domains.items()
if key in set(edge_properties_and_domains)
}),
)
for edge_id, edge_setting in old_edge_settings.items()
}
new_node_settings = {
node_id: NodeSettings(
conservation_rules=node_rules,
rule_priorities=node_setting.rule_priorities,
qn_domains=({
key: val
for key, val in node_setting.qn_domains.items()
if key in set(node_properties_and_domains)
}),
)
for node_id, node_setting in old_node_settings.items()
}
new_combined_settings = MutableTransition(
topology=quantum_number_problem_set.solving_settings.topology,
states=new_edge_settings,
interactions=new_node_settings,
)
new_edge_properties = {
edge_id: {
edge_quantum_number: scalar
for edge_quantum_number, scalar in graph_edge_property_map.items()
if edge_quantum_number in edge_properties_and_domains
}
for edge_id, graph_edge_property_map in old_edge_properties.items()
}
new_node_properties = {
node_id: {
node_quantum_number: scalar
for node_quantum_number, scalar in graph_node_property_map.items()
if node_quantum_number in node_properties_and_domains
}
for node_id, graph_node_property_map in old_node_properties.items()
}
new_combined_properties = MutableTransition(
topology=quantum_number_problem_set.initial_facts.topology,
states=new_edge_properties,
interactions=new_node_properties,
)
return attrs.evolve(
quantum_number_problem_set,
solving_settings=new_combined_settings,
initial_facts=new_combined_properties,
)


QuantumNumberSolution = MutableTransition[GraphEdgePropertyMap, GraphNodePropertyMap]


Expand Down
Loading
Loading