Skip to content

Commit 782ee0c

Browse files
committed
:q
ºPrint statements
1 parent c19ba00 commit 782ee0c

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

src/axiomatic/pic_helpers.py

Lines changed: 80 additions & 3 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):
@@ -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,80 @@ 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+
211+
validation = StatementValidationDictionary(
212+
cost_functions=(validation.cost_functions if validation is not None else None) or [StatementValidation()]*len(statements.cost_functions or []),
213+
parameter_constraints=(validation.parameter_constraints if validation is not None else None) or [StatementValidation()]*len(statements.parameter_constraints or []),
214+
structure_constraints=(validation.structure_constraints if validation is not None else None) or [StatementValidation()]*len(statements.structure_constraints or []),
215+
unformalizable_statements=(validation.unformalizable_statements if validation is not None else None) or [StatementValidation()]*len(statements.unformalizable_statements or [])
216+
)
217+
218+
if len(validation.cost_functions or []) != len(statements.cost_functions or []):
219+
raise ValueError("Number of cost functions and validations do not match.")
220+
if len(validation.parameter_constraints or []) != len(statements.parameter_constraints or []):
221+
raise ValueError("Number of parameter constraints and validations do not match.")
222+
if len(validation.structure_constraints or []) != len(statements.structure_constraints or []):
223+
raise ValueError("Number of structure constraints and validations do not match.")
224+
if len(validation.unformalizable_statements or []) != len(statements.unformalizable_statements or []):
225+
raise ValueError("Number of unformalizable statements and validations do not match.")
226+
227+
print("-----------------------------------\n")
228+
for z3_stmt, z3_val in zip((statements.cost_functions or []) + (statements.parameter_constraints or []), (validation.cost_functions or []) + (validation.parameter_constraints or [])):
229+
print("Type:", z3_stmt.type)
230+
print("Statement:", z3_stmt.text)
231+
print("Formalization:", end=" ")
232+
if z3_stmt.formalization is None:
233+
print("UNFORMALIZED")
234+
else:
235+
code = z3_stmt.formalization.code
236+
if z3_stmt.formalization.mapping is not None:
237+
for var_name, computation in z3_stmt.formalization.mapping.items():
238+
if computation is not None:
239+
args_str = ", ".join(
240+
[
241+
f"{argname}="
242+
+ (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
243+
for argname, argvalue in computation.arguments.items()
244+
]
245+
)
246+
code = code.replace(var_name, f"{computation.name}({args_str})")
247+
print(code)
248+
val = z3_stmt.validation or z3_val
249+
if z3_stmt.type == "COST_FUNCTION":
250+
print(f"Satisfiable: {val.satisfiable}")
251+
print(val.message)
252+
else:
253+
print(f"Satisfiable: {val.satisfiable}")
254+
print(f"Holds: {val.holds} ({val.message})")
255+
print("\n-----------------------------------\n")
256+
for struct_stmt, struct_val in zip(statements.structure_constraints or [], validation.structure_constraints or []):
257+
print("Type:", struct_stmt.type)
258+
print("Statement:", struct_stmt.text)
259+
print("Formalization:", end=" ")
260+
if struct_stmt.formalization is None:
261+
print("UNFORMALIZED")
262+
else:
263+
func_constr = struct_stmt.formalization
264+
args_str = ", ".join(
265+
[
266+
f"{argname}=" + (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
267+
for argname, argvalue in func_constr.arguments.items()
268+
]
269+
)
270+
func_str = f"{func_constr.function_name}({args_str}) == {func_constr.expected_result}"
271+
print(func_str)
272+
val = struct_stmt.validation or struct_val
273+
print(f"Satisfiable: {val.satisfiable}")
274+
print(f"Holds: {val.holds}")
275+
print("\n-----------------------------------\n")
276+
for unf_stmt in statements.unformalizable_statements or []:
277+
print("Type:", unf_stmt.type)
278+
print("Statement:", unf_stmt.text)
279+
print("Formalization: UNFORMALIZABLE")
280+
print("\n-----------------------------------\n")

0 commit comments

Comments
 (0)