Skip to content

Commit 24b925e

Browse files
committed
:q
ºPrint statements
1 parent c19ba00 commit 24b925e

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

src/axiomatic/pic_helpers.py

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ipywidgets import interactive, IntSlider # type: ignore
44
from typing import List, Optional
55

6-
from . import Parameter
6+
from . import Parameter, StatementDictionary, StatementValidationDictionary, StatementValidation
77

88

99
def plot_circuit(component):
@@ -80,8 +80,8 @@ def plot_single_spectrum(
8080
"""
8181
Plot a single spectrum with vertical and horizontal lines.
8282
"""
83-
hlines = hlines or []
84-
vlines = vlines or []
83+
hlines = hlines
84+
vlines = vlines
8585

8686
plt.clf()
8787
plt.figure(figsize=(10, 5))
@@ -142,8 +142,8 @@ def plot_interactive_spectra(
142142

143143
slider_index = slider_index or list(range(len(spectra[0])))
144144
spectrum_labels = spectrum_labels or [f"Spectrum {i}" for i in range(len(spectra))]
145-
vlines = vlines or []
146-
hlines = hlines or []
145+
vlines = vlines
146+
hlines = hlines
147147

148148
# Function to update the plot
149149
def plot_array(index=0):
@@ -201,3 +201,86 @@ def plot_parameter_history(parameters: List[Parameter], parameter_history: List[
201201
]
202202
)
203203
plt.show()
204+
205+
206+
def print_statements(statements: StatementDictionary, validation: Optional[StatementValidationDictionary] = None):
207+
"""
208+
Print a list of statements in nice readable format.
209+
"""
210+
statements = StatementDictionary(
211+
cost_functions=statements.cost_functions or [],
212+
parameter_constraints=statements.parameter_constraints or [],
213+
structure_constraints=statements.structure_constraints or [],
214+
unformalizable_statements=statements.unformalizable_statements or [],
215+
)
216+
217+
validation = StatementValidationDictionary(
218+
cost_functions=(validation.cost_functions if validation is not None else None) or [StatementValidation()]*len(statements.cost_functions),
219+
parameter_constraints=(validation.parameter_constraints if validation is not None else None) or [StatementValidation()]*len(statements.parameter_constraints),
220+
structure_constraints=(validation.structure_constraints if validation is not None else None) or [StatementValidation()]*len(statements.structure_constraints),
221+
unformalizable_statements=(validation.unformalizable_statements if validation is not None else None) or [StatementValidation()]*len(statements.unformalizable_statements)
222+
)
223+
224+
if len(validation.cost_functions) != len(statements.cost_functions):
225+
raise ValueError("Number of cost functions and validations do not match.")
226+
if len(validation.parameter_constraints) != len(statements.parameter_constraints):
227+
raise ValueError("Number of parameter constraints and validations do not match.")
228+
if len(validation.structure_constraints) != len(statements.structure_constraints):
229+
raise ValueError("Number of structure constraints and validations do not match.")
230+
if len(validation.unformalizable_statements) != len(statements.unformalizable_statements):
231+
raise ValueError("Number of unformalizable statements and validations do not match.")
232+
233+
print("-----------------------------------\n")
234+
for z3_stmt, z3_val in zip((statements.cost_functions) + (statements.parameter_constraints), (validation.cost_functions) + (validation.parameter_constraints)):
235+
print("Type:", z3_stmt.type)
236+
print("Statement:", z3_stmt.text)
237+
print("Formalization:", end=" ")
238+
if z3_stmt.formalization is None:
239+
print("UNFORMALIZED")
240+
else:
241+
code = z3_stmt.formalization.code
242+
if z3_stmt.formalization.mapping is not None:
243+
for var_name, computation in z3_stmt.formalization.mapping.items():
244+
if computation is not None:
245+
args_str = ", ".join(
246+
[
247+
f"{argname}="
248+
+ (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
249+
for argname, argvalue in computation.arguments.items()
250+
]
251+
)
252+
code = code.replace(var_name, f"{computation.name}({args_str})")
253+
print(code)
254+
val = z3_stmt.validation or z3_val
255+
if z3_stmt.type == "COST_FUNCTION":
256+
print(f"Satisfiable: {val.satisfiable}")
257+
print(val.message)
258+
else:
259+
print(f"Satisfiable: {val.satisfiable}")
260+
print(f"Holds: {val.holds} ({val.message})")
261+
print("\n-----------------------------------\n")
262+
for struct_stmt, struct_val in zip(statements.structure_constraints, validation.structure_constraints):
263+
print("Type:", struct_stmt.type)
264+
print("Statement:", struct_stmt.text)
265+
print("Formalization:", end=" ")
266+
if struct_stmt.formalization is None:
267+
print("UNFORMALIZED")
268+
else:
269+
func_constr = struct_stmt.formalization
270+
args_str = ", ".join(
271+
[
272+
f"{argname}=" + (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
273+
for argname, argvalue in func_constr.arguments.items()
274+
]
275+
)
276+
func_str = f"{func_constr.function_name}({args_str}) == {func_constr.expected_result}"
277+
print(func_str)
278+
val = struct_stmt.validation or struct_val
279+
print(f"Satisfiable: {val.satisfiable}")
280+
print(f"Holds: {val.holds}")
281+
print("\n-----------------------------------\n")
282+
for unf_stmt in statements.unformalizable_statements:
283+
print("Type:", unf_stmt.type)
284+
print("Statement:", unf_stmt.text)
285+
print("Formalization: UNFORMALIZABLE")
286+
print("\n-----------------------------------\n")

0 commit comments

Comments
 (0)