classiq 0.45.1__py3-none-any.whl → 0.46.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. classiq/__init__.py +0 -1
  2. classiq/_internals/__init__.py +20 -0
  3. classiq/_internals/authentication/authentication.py +11 -0
  4. classiq/analyzer/analyzer.py +12 -10
  5. classiq/applications/combinatorial_helpers/combinatorial_problem_utils.py +1 -1
  6. classiq/applications/combinatorial_helpers/pauli_helpers/pauli_utils.py +1 -1
  7. classiq/applications/combinatorial_optimization/combinatorial_optimization_model_constructor.py +1 -1
  8. classiq/applications/libraries/qmci_library.py +4 -9
  9. classiq/execution/execution_session.py +68 -7
  10. classiq/executor.py +14 -2
  11. classiq/interface/_version.py +1 -1
  12. classiq/interface/backend/backend_preferences.py +189 -0
  13. classiq/interface/backend/quantum_backend_providers.py +38 -0
  14. classiq/interface/debug_info/debug_info.py +22 -2
  15. classiq/interface/exceptions.py +16 -1
  16. classiq/interface/executor/execution_preferences.py +18 -0
  17. classiq/interface/generator/application_apis/chemistry_declarations.py +1 -177
  18. classiq/interface/generator/application_apis/combinatorial_optimization_declarations.py +0 -12
  19. classiq/interface/generator/application_apis/finance_declarations.py +8 -43
  20. classiq/interface/generator/application_apis/qsvm_declarations.py +0 -78
  21. classiq/interface/generator/builtin_api_builder.py +0 -3
  22. classiq/interface/generator/functions/__init__.py +0 -2
  23. classiq/interface/generator/functions/builtins/__init__.py +0 -15
  24. classiq/interface/generator/generated_circuit_data.py +2 -0
  25. classiq/interface/generator/hardware/hardware_data.py +37 -0
  26. classiq/interface/generator/model/constraints.py +18 -1
  27. classiq/interface/generator/model/preferences/preferences.py +53 -1
  28. classiq/interface/generator/model/quantum_register.py +1 -1
  29. classiq/interface/generator/quantum_program.py +10 -2
  30. classiq/interface/generator/transpiler_basis_gates.py +4 -0
  31. classiq/interface/generator/types/builtin_enum_declarations.py +136 -21
  32. classiq/interface/generator/types/enum_declaration.py +1 -3
  33. classiq/interface/generator/types/struct_declaration.py +1 -3
  34. classiq/interface/hardware.py +5 -0
  35. classiq/interface/ide/visual_model.py +1 -1
  36. classiq/interface/model/classical_parameter_declaration.py +6 -0
  37. classiq/interface/model/inplace_binary_operation.py +0 -14
  38. classiq/interface/model/model.py +1 -18
  39. classiq/interface/model/port_declaration.py +4 -2
  40. classiq/interface/model/quantum_function_declaration.py +19 -6
  41. classiq/interface/model/quantum_lambda_function.py +11 -1
  42. classiq/interface/model/quantum_variable_declaration.py +1 -1
  43. classiq/model_expansions/__init__.py +0 -0
  44. classiq/model_expansions/atomic_expression_functions_defs.py +250 -0
  45. classiq/model_expansions/capturing/__init__.py +0 -0
  46. classiq/model_expansions/capturing/captured_var_manager.py +50 -0
  47. classiq/model_expansions/capturing/mangling_utils.py +17 -0
  48. classiq/model_expansions/capturing/propagated_var_stack.py +180 -0
  49. classiq/model_expansions/closure.py +160 -0
  50. classiq/model_expansions/debug_flag.py +3 -0
  51. classiq/model_expansions/evaluators/__init__.py +0 -0
  52. classiq/model_expansions/evaluators/arg_type_match.py +160 -0
  53. classiq/model_expansions/evaluators/argument_types.py +42 -0
  54. classiq/model_expansions/evaluators/classical_expression.py +36 -0
  55. classiq/model_expansions/evaluators/control.py +144 -0
  56. classiq/model_expansions/evaluators/parameter_types.py +227 -0
  57. classiq/model_expansions/evaluators/quantum_type_utils.py +235 -0
  58. classiq/model_expansions/evaluators/type_type_match.py +90 -0
  59. classiq/model_expansions/expression_evaluator.py +125 -0
  60. classiq/model_expansions/expression_renamer.py +76 -0
  61. classiq/model_expansions/function_builder.py +192 -0
  62. classiq/model_expansions/generative_functions.py +101 -0
  63. classiq/model_expansions/interpreter.py +365 -0
  64. classiq/model_expansions/model_tables.py +105 -0
  65. classiq/model_expansions/quantum_operations/__init__.py +19 -0
  66. classiq/model_expansions/quantum_operations/bind.py +64 -0
  67. classiq/model_expansions/quantum_operations/classicalif.py +39 -0
  68. classiq/model_expansions/quantum_operations/control.py +235 -0
  69. classiq/model_expansions/quantum_operations/emitter.py +215 -0
  70. classiq/model_expansions/quantum_operations/expression_operation.py +218 -0
  71. classiq/model_expansions/quantum_operations/inplace_binary_operation.py +250 -0
  72. classiq/model_expansions/quantum_operations/invert.py +38 -0
  73. classiq/model_expansions/quantum_operations/power.py +74 -0
  74. classiq/model_expansions/quantum_operations/quantum_assignment_operation.py +174 -0
  75. classiq/model_expansions/quantum_operations/quantum_function_call.py +15 -0
  76. classiq/model_expansions/quantum_operations/repeat.py +33 -0
  77. classiq/model_expansions/quantum_operations/variable_decleration.py +28 -0
  78. classiq/model_expansions/quantum_operations/within_apply.py +46 -0
  79. classiq/model_expansions/scope.py +226 -0
  80. classiq/model_expansions/scope_initialization.py +136 -0
  81. classiq/model_expansions/sympy_conversion/__init__.py +0 -0
  82. classiq/model_expansions/sympy_conversion/arithmetics.py +49 -0
  83. classiq/model_expansions/sympy_conversion/expression_to_sympy.py +150 -0
  84. classiq/model_expansions/sympy_conversion/sympy_to_python.py +113 -0
  85. classiq/model_expansions/utils/__init__.py +0 -0
  86. classiq/model_expansions/utils/counted_name_allocator.py +11 -0
  87. classiq/model_expansions/visitors/__init__.py +0 -0
  88. classiq/model_expansions/visitors/boolean_expression_transformers.py +214 -0
  89. classiq/model_expansions/visitors/variable_references.py +115 -0
  90. classiq/qmod/__init__.py +1 -3
  91. classiq/qmod/builtins/enums.py +33 -2
  92. classiq/qmod/builtins/functions/__init__.py +251 -0
  93. classiq/qmod/builtins/functions/amplitude_estimation.py +26 -0
  94. classiq/qmod/builtins/functions/arithmetic.py +68 -0
  95. classiq/qmod/builtins/functions/benchmarking.py +8 -0
  96. classiq/qmod/builtins/functions/chemistry.py +91 -0
  97. classiq/qmod/builtins/functions/discrete_sine_cosine_transform.py +105 -0
  98. classiq/qmod/builtins/functions/exponentiation.py +110 -0
  99. classiq/qmod/builtins/functions/finance.py +34 -0
  100. classiq/qmod/builtins/functions/grover.py +179 -0
  101. classiq/qmod/builtins/functions/hea.py +59 -0
  102. classiq/qmod/builtins/functions/linear_pauli_rotation.py +65 -0
  103. classiq/qmod/builtins/functions/modular_exponentiation.py +137 -0
  104. classiq/qmod/builtins/functions/operators.py +22 -0
  105. classiq/qmod/builtins/functions/qaoa_penalty.py +116 -0
  106. classiq/qmod/builtins/functions/qft.py +23 -0
  107. classiq/qmod/builtins/functions/qpe.py +39 -0
  108. classiq/qmod/builtins/functions/qsvm.py +24 -0
  109. classiq/qmod/builtins/functions/qsvt.py +136 -0
  110. classiq/qmod/builtins/functions/standard_gates.py +739 -0
  111. classiq/qmod/builtins/functions/state_preparation.py +356 -0
  112. classiq/qmod/builtins/functions/swap_test.py +25 -0
  113. classiq/qmod/builtins/structs.py +50 -28
  114. classiq/qmod/cparam.py +64 -0
  115. classiq/qmod/create_model_function.py +190 -0
  116. classiq/qmod/declaration_inferrer.py +52 -81
  117. classiq/qmod/expression_query.py +16 -0
  118. classiq/qmod/generative.py +48 -0
  119. classiq/qmod/model_state_container.py +1 -2
  120. classiq/qmod/native/pretty_printer.py +7 -11
  121. classiq/qmod/pretty_print/pretty_printer.py +7 -11
  122. classiq/qmod/python_classical_type.py +67 -0
  123. classiq/qmod/qfunc.py +19 -4
  124. classiq/qmod/qmod_parameter.py +15 -64
  125. classiq/qmod/qmod_variable.py +27 -45
  126. classiq/qmod/quantum_callable.py +1 -1
  127. classiq/qmod/quantum_expandable.py +10 -4
  128. classiq/qmod/quantum_function.py +22 -40
  129. classiq/qmod/semantics/error_manager.py +22 -10
  130. classiq/qmod/semantics/static_semantics_visitor.py +10 -12
  131. classiq/qmod/semantics/validation/types_validation.py +6 -7
  132. classiq/qmod/utilities.py +2 -2
  133. classiq/qmod/write_qmod.py +14 -0
  134. classiq/show.py +10 -0
  135. classiq/synthesis.py +46 -2
  136. {classiq-0.45.1.dist-info → classiq-0.46.0.dist-info}/METADATA +1 -1
  137. {classiq-0.45.1.dist-info → classiq-0.46.0.dist-info}/RECORD +138 -74
  138. classiq/interface/generator/functions/builtins/core_library/__init__.py +0 -16
  139. classiq/interface/generator/functions/builtins/core_library/atomic_quantum_functions.py +0 -710
  140. classiq/interface/generator/functions/builtins/core_library/exponentiation_functions.py +0 -105
  141. classiq/interface/generator/functions/builtins/open_lib_functions.py +0 -2489
  142. classiq/interface/generator/functions/builtins/quantum_operators.py +0 -24
  143. classiq/interface/generator/types/builtin_struct_declarations/__init__.py +0 -1
  144. classiq/interface/generator/types/builtin_struct_declarations/pauli_struct_declarations.py +0 -21
  145. classiq/qmod/builtins/functions.py +0 -1029
  146. {classiq-0.45.1.dist-info → classiq-0.46.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,160 @@
1
+ from enum import Enum
2
+ from typing import Any, List, Sequence
3
+
4
+ from classiq.interface.exceptions import ClassiqExpansionError
5
+ from classiq.interface.generator.expressions.qmod_sized_proxy import QmodSizedProxy
6
+ from classiq.interface.generator.expressions.qmod_struct_instance import (
7
+ QmodStructInstance,
8
+ )
9
+ from classiq.interface.generator.functions.classical_type import (
10
+ StructMetaType,
11
+ )
12
+ from classiq.interface.generator.functions.concrete_types import ConcreteClassicalType
13
+ from classiq.interface.generator.functions.type_name import (
14
+ TypeName,
15
+ )
16
+ from classiq.interface.model.classical_parameter_declaration import (
17
+ AnonClassicalParameterDeclaration,
18
+ )
19
+ from classiq.interface.model.port_declaration import AnonPortDeclaration
20
+ from classiq.interface.model.quantum_function_declaration import (
21
+ AnonPositionalArg,
22
+ AnonQuantumOperandDeclaration,
23
+ )
24
+
25
+ from classiq.model_expansions.closure import FunctionClosure
26
+ from classiq.model_expansions.evaluators.type_type_match import check_signature_match
27
+ from classiq.model_expansions.model_tables import SymbolTable
28
+ from classiq.model_expansions.scope import Evaluated, QuantumSymbol
29
+ from classiq.qmod.qmod_parameter import CInt, get_qmod_type
30
+
31
+
32
+ def check_type_match(
33
+ parameters: Sequence[AnonPositionalArg],
34
+ arguments: List[Evaluated],
35
+ function_name: str,
36
+ ) -> None:
37
+ if len(parameters) != len(arguments):
38
+ raise ClassiqExpansionError(
39
+ f"Function {function_name!r} takes {len(parameters)} arguments but "
40
+ f"{len(arguments)} were given"
41
+ )
42
+ for parameter, evaluated_arg in zip(parameters, arguments):
43
+ check_arg_type_match(evaluated_arg.value, parameter, function_name)
44
+
45
+
46
+ def check_arg_type_match(
47
+ argument: Any, parameter: AnonPositionalArg, function_name: str
48
+ ) -> None:
49
+ message_prefix = (
50
+ f"Argument {str(argument)!r} to parameter {parameter.name!r} of function "
51
+ f"{function_name!r} has incompatible type; "
52
+ )
53
+ if isinstance(parameter, AnonPortDeclaration):
54
+ error_message = message_prefix + "expected quantum variable"
55
+ _check_qvar_type_match(argument, error_message)
56
+ elif isinstance(parameter, AnonQuantumOperandDeclaration):
57
+ if parameter.is_list:
58
+ error_message = message_prefix + "expected list of operands"
59
+ _check_operand_list_type_match(
60
+ argument, parameter, function_name, error_message
61
+ )
62
+ else:
63
+ error_message = message_prefix + "expected operand"
64
+ _check_operand_type_match(argument, parameter, function_name, error_message)
65
+ elif isinstance(parameter, AnonClassicalParameterDeclaration):
66
+ error_message = (
67
+ message_prefix + f"expected {_resolve_type_name(parameter.classical_type)}"
68
+ )
69
+ _check_classical_type_match(argument, parameter, error_message)
70
+ else:
71
+ raise ClassiqExpansionError(
72
+ f"unexpected parameter declaration type: {type(parameter).__name__}"
73
+ )
74
+
75
+
76
+ def _check_qvar_type_match(argument: Any, error_message: str) -> None:
77
+ if not isinstance(argument, QuantumSymbol):
78
+ raise ClassiqExpansionError(error_message)
79
+
80
+
81
+ def _check_operand_list_type_match(
82
+ argument: Any,
83
+ parameter: AnonQuantumOperandDeclaration,
84
+ function_name: str,
85
+ error_message: str,
86
+ ) -> None:
87
+ if not isinstance(argument, list) or any(
88
+ not isinstance(op, FunctionClosure) for op in argument
89
+ ):
90
+ raise ClassiqExpansionError(error_message)
91
+ for idx, operand in enumerate(argument):
92
+ if operand.positional_arg_declarations is not None:
93
+ check_signature_match(
94
+ parameter.positional_arg_declarations,
95
+ operand.positional_arg_declarations,
96
+ f"operand #{idx + 1} in parameter {parameter.name!r} "
97
+ f"in function {function_name!r}",
98
+ )
99
+
100
+
101
+ def _check_operand_type_match(
102
+ argument: Any,
103
+ parameter: AnonQuantumOperandDeclaration,
104
+ function_name: str,
105
+ error_message: str,
106
+ ) -> None:
107
+ if not isinstance(argument, FunctionClosure):
108
+ raise ClassiqExpansionError(error_message)
109
+ if argument.positional_arg_declarations is not None:
110
+ check_signature_match(
111
+ parameter.positional_arg_declarations,
112
+ argument.positional_arg_declarations,
113
+ f"operand {parameter.name!r} in function {function_name!r}",
114
+ )
115
+
116
+
117
+ def _check_classical_type_match(
118
+ argument: Any, parameter: AnonClassicalParameterDeclaration, error_message: str
119
+ ) -> None:
120
+ classical_type = parameter.classical_type
121
+ type_name = _resolve_type_name(classical_type)
122
+ type_is_struct = (
123
+ isinstance(classical_type, TypeName)
124
+ and classical_type.name in SymbolTable.type_table
125
+ )
126
+ type_is_enum = (
127
+ isinstance(classical_type, TypeName)
128
+ and classical_type.name in SymbolTable.enum_table
129
+ )
130
+ arg_is_qvar = isinstance(argument, QmodSizedProxy)
131
+ arg_is_builtin = argument.__class__.__module__ == "builtins"
132
+ arg_is_enum = isinstance(argument, Enum)
133
+ arg_is_struct = isinstance(argument, QmodStructInstance)
134
+ arg_struct_name = None if not arg_is_struct else argument.struct_declaration.name
135
+ if (
136
+ arg_is_qvar
137
+ or arg_is_builtin
138
+ and (type_is_struct or type_is_enum)
139
+ or arg_is_struct
140
+ and (not type_is_struct or arg_struct_name != type_name)
141
+ or arg_is_enum
142
+ and get_qmod_type(classical_type) != CInt
143
+ and (not type_is_enum or type(argument).__name__ != type_name)
144
+ ):
145
+ raise ClassiqExpansionError(error_message)
146
+
147
+
148
+ def _resolve_type_name(classical_type: ConcreteClassicalType) -> str:
149
+ if isinstance(classical_type, StructMetaType):
150
+ type_name = "Struct"
151
+ else:
152
+ type_name = get_qmod_type(classical_type).__name__
153
+ if not isinstance(classical_type, TypeName):
154
+ return type_name
155
+ if (
156
+ type_name not in SymbolTable.type_table
157
+ and type_name not in SymbolTable.enum_table
158
+ ):
159
+ raise ClassiqExpansionError(f"Undefined type {type_name}")
160
+ return type_name
@@ -0,0 +1,42 @@
1
+ from typing import List, Sequence
2
+
3
+ from classiq.interface.generator.functions.port_declaration import (
4
+ PortDeclarationDirection,
5
+ )
6
+ from classiq.interface.model.port_declaration import AnonPortDeclaration
7
+ from classiq.interface.model.quantum_function_declaration import AnonPositionalArg
8
+
9
+ from classiq.model_expansions.evaluators.quantum_type_utils import copy_type_information
10
+ from classiq.model_expansions.scope import Evaluated, QuantumSymbol
11
+
12
+
13
+ def add_information_from_output_arguments(
14
+ parameters: Sequence[AnonPositionalArg],
15
+ args: List[Evaluated],
16
+ ) -> List[Evaluated]:
17
+ """
18
+ This function propagates the quantum type information from the output arguments
19
+ to the arguments that were passed to it.
20
+ Example:
21
+ ...
22
+ p = QArray("p", QBit)
23
+ allocate(4, p)
24
+ other statements with p...
25
+
26
+ In the other statements with p, the size of 4 will be part of p's type info.
27
+ """
28
+ for parameter, argument in zip(parameters, args):
29
+ if not isinstance(parameter, AnonPortDeclaration):
30
+ continue
31
+
32
+ argument_as_quantum_symbol = argument.as_type(QuantumSymbol)
33
+
34
+ if parameter.direction != PortDeclarationDirection.Output:
35
+ continue
36
+
37
+ copy_type_information(
38
+ parameter.quantum_type,
39
+ argument_as_quantum_symbol.quantum_type,
40
+ str(argument_as_quantum_symbol.handle),
41
+ )
42
+ return args
@@ -0,0 +1,36 @@
1
+ from typing import get_args
2
+
3
+ from classiq.interface.generator.expressions.evaluated_expression import (
4
+ EvaluatedExpression,
5
+ )
6
+ from classiq.interface.generator.expressions.expression import Expression
7
+ from classiq.interface.generator.expressions.expression_types import ExpressionValue
8
+ from classiq.interface.model.handle_binding import HandleBinding
9
+
10
+ from classiq.model_expansions.expression_evaluator import evaluate
11
+ from classiq.model_expansions.scope import Evaluated, QuantumSymbol, Scope
12
+
13
+
14
+ def evaluate_classical_expression(expr: Expression, scope: Scope) -> Evaluated:
15
+ all_symbols = scope.items()
16
+ locals_dict = {
17
+ name: EvaluatedExpression(value=evaluated.value)
18
+ for name, evaluated in all_symbols
19
+ if isinstance(evaluated.value, get_args(ExpressionValue))
20
+ } | {
21
+ name: EvaluatedExpression(
22
+ value=evaluated.value.quantum_type.get_proxy(HandleBinding(name=name))
23
+ )
24
+ for name, evaluated in all_symbols
25
+ if isinstance(evaluated.value, QuantumSymbol)
26
+ and evaluated.value.quantum_type.has_size_in_bits
27
+ }
28
+ uninitialized_locals = {
29
+ name
30
+ for name, evaluated in all_symbols
31
+ if isinstance(evaluated.value, QuantumSymbol)
32
+ and not evaluated.value.quantum_type.has_size_in_bits
33
+ }
34
+
35
+ ret = evaluate(expr, locals_dict, uninitialized_locals)
36
+ return Evaluated(value=ret.value)
@@ -0,0 +1,144 @@
1
+ from typing import Any, Tuple
2
+
3
+ from sympy import Equality
4
+ from sympy.core.numbers import Number
5
+
6
+ from classiq.interface.exceptions import ClassiqExpansionError
7
+ from classiq.interface.generator.expressions.qmod_qscalar_proxy import (
8
+ QmodQNumProxy,
9
+ QmodQScalarProxy,
10
+ QmodSizedProxy,
11
+ )
12
+
13
+ CONTROL_INOUT_NAME = "ctrl"
14
+
15
+
16
+ def type_name(obj: Any) -> str:
17
+ if isinstance(obj, QmodSizedProxy):
18
+ return obj.type_name
19
+ return type(obj).__name__
20
+
21
+
22
+ def resolve_num_condition(condition: Equality) -> Tuple[QmodSizedProxy, str]:
23
+ ctrl, ctrl_val = condition.args
24
+ if isinstance(ctrl, Number) and isinstance(ctrl_val, QmodQScalarProxy):
25
+ ctrl, ctrl_val = ctrl_val, ctrl
26
+ if not isinstance(ctrl, QmodSizedProxy) or not (isinstance(ctrl_val, Number)):
27
+ _raise_numeric_condition_error(ctrl, ctrl_val)
28
+ return ctrl, _calculate_ctrl_state(ctrl, float(ctrl_val))
29
+
30
+
31
+ def _calculate_ctrl_state(ctrl: QmodSizedProxy, ctrl_val: float) -> str:
32
+ is_signed, fraction_places = _get_numeric_attributes(ctrl)
33
+
34
+ integer_ctrl_val = _get_integer_ctrl_val(ctrl, ctrl_val, fraction_places)
35
+
36
+ _validate_control_value_sign(ctrl, integer_ctrl_val, is_signed)
37
+ _validate_control_var_qubits(
38
+ ctrl, integer_ctrl_val, is_signed, fraction_places, ctrl_val
39
+ )
40
+
41
+ return _to_twos_complement(integer_ctrl_val, ctrl.size)
42
+
43
+
44
+ def _get_numeric_attributes(ctrl: QmodSizedProxy) -> Tuple[bool, int]:
45
+ return (
46
+ (ctrl.is_signed, ctrl.fraction_digits)
47
+ if isinstance(ctrl, QmodQNumProxy)
48
+ else (False, 0)
49
+ )
50
+
51
+
52
+ def _get_integer_ctrl_val(
53
+ ctrl: QmodSizedProxy, ctrl_val: float, fraction_places: int
54
+ ) -> int:
55
+ unfractioned_ctrl_val = ctrl_val * 2**fraction_places
56
+ if unfractioned_ctrl_val != int(unfractioned_ctrl_val):
57
+ raise ClassiqExpansionError(
58
+ f"Variable {str(ctrl)!r} doesne't have enough fraction digits to "
59
+ f"represent control value {ctrl_val}"
60
+ )
61
+ return int(unfractioned_ctrl_val)
62
+
63
+
64
+ def _validate_control_value_sign(
65
+ ctrl: QmodSizedProxy, ctrl_val: int, is_signed: bool
66
+ ) -> None:
67
+ if not is_signed and ctrl_val < 0:
68
+ raise ClassiqExpansionError(
69
+ f"Variable {str(ctrl)!r} is not signed but control value "
70
+ f"{ctrl_val} is negative"
71
+ )
72
+
73
+
74
+ def _validate_control_var_qubits(
75
+ ctrl: QmodSizedProxy,
76
+ ctrl_val: int,
77
+ is_signed: bool,
78
+ fraction_places: int,
79
+ orig_ctrl_val: float,
80
+ ) -> None:
81
+ required_qubits = _min_bit_length(ctrl_val, is_signed)
82
+ fraction_places_message = (
83
+ f" with {fraction_places} fraction digits" if fraction_places else ""
84
+ )
85
+ if ctrl.size < required_qubits:
86
+ raise ClassiqExpansionError(
87
+ f"Variable {str(ctrl)!r} has {ctrl.size} qubits{fraction_places_message} but control value "
88
+ f"{str(orig_ctrl_val if fraction_places else ctrl_val)!r} requires at least {required_qubits} qubits{fraction_places_message}"
89
+ )
90
+
91
+
92
+ def _raise_numeric_condition_error(ctrl: Any, ctrl_val: Any) -> None:
93
+ message = (
94
+ "Control condition must be of the form '<quantum-variable> == "
95
+ "<classical-number-expression>' or vice versa. "
96
+ )
97
+ prefix = f"Neither {ctrl!r} (type {type_name(ctrl)}) or {ctrl_val!r} (type {type_name(ctrl_val)}) is a "
98
+ if not isinstance(ctrl, QmodSizedProxy) and not isinstance(
99
+ ctrl_val, QmodSizedProxy
100
+ ):
101
+ message += prefix + "quantum variable."
102
+ elif not isinstance(ctrl, Number) and not isinstance(ctrl_val, Number):
103
+ message += prefix + "classical number."
104
+ raise ClassiqExpansionError(message)
105
+
106
+
107
+ def _min_unsigned_bit_length(number: int) -> int:
108
+ if number < 0:
109
+ raise ClassiqExpansionError(
110
+ f"Quantum register is not signed but control value "
111
+ f"'{number}' is negative"
112
+ )
113
+ try:
114
+ return 1 if number == 0 else number.bit_length()
115
+ except AttributeError as e:
116
+ raise e
117
+
118
+
119
+ def _min_signed_bit_length(number: int) -> int:
120
+ pos_val = abs(number)
121
+ is_whole = pos_val & (pos_val - 1) == 0
122
+ if number <= 0 and is_whole:
123
+ return _min_unsigned_bit_length(pos_val)
124
+ return _min_unsigned_bit_length(pos_val) + 1
125
+
126
+
127
+ def _min_bit_length(number: int, is_signed: bool) -> int:
128
+ return (
129
+ _min_signed_bit_length(number)
130
+ if is_signed
131
+ else _min_unsigned_bit_length(number)
132
+ )
133
+
134
+
135
+ def _to_twos_complement(value: int, bits: int) -> str:
136
+ if value >= 0:
137
+ return bin(value)[2:].zfill(bits)[::-1]
138
+ return _to_negative_twos_complement(value, bits)
139
+
140
+
141
+ def _to_negative_twos_complement(value: int, bits: int) -> str:
142
+ mask = (1 << bits) - 1
143
+ value = (abs(value) ^ mask) + 1
144
+ return bin(value)[:1:-1].rjust(bits, "1")
@@ -0,0 +1,227 @@
1
+ from typing import List, Set, Union
2
+
3
+ from classiq.interface.exceptions import (
4
+ ClassiqExpansionError,
5
+ ClassiqInternalExpansionError,
6
+ )
7
+ from classiq.interface.generator.functions.concrete_types import ConcreteQuantumType
8
+ from classiq.interface.generator.functions.port_declaration import (
9
+ PortDeclarationDirection,
10
+ )
11
+ from classiq.interface.generator.functions.type_name import (
12
+ TypeName,
13
+ )
14
+ from classiq.interface.model.port_declaration import PortDeclaration
15
+ from classiq.interface.model.quantum_function_declaration import (
16
+ PositionalArg,
17
+ QuantumOperandDeclaration,
18
+ )
19
+ from classiq.interface.model.quantum_type import (
20
+ QuantumBit,
21
+ QuantumBitvector,
22
+ QuantumNumeric,
23
+ QuantumType,
24
+ )
25
+
26
+ from classiq.model_expansions.closure import FunctionClosure
27
+ from classiq.model_expansions.evaluators.arg_type_match import check_type_match
28
+ from classiq.model_expansions.evaluators.classical_expression import (
29
+ evaluate_classical_expression,
30
+ )
31
+ from classiq.model_expansions.evaluators.quantum_type_utils import (
32
+ copy_type_information,
33
+ set_element_type,
34
+ set_fraction_digits,
35
+ set_is_signed,
36
+ set_length,
37
+ set_size,
38
+ set_struct_fields,
39
+ )
40
+ from classiq.model_expansions.scope import Evaluated, QuantumSymbol, Scope
41
+
42
+
43
+ def evaluate_parameter_types_from_args(
44
+ closure: FunctionClosure, signature_scope: Scope, arguments: List[Evaluated]
45
+ ) -> List[PositionalArg]:
46
+ parameters = closure.positional_arg_declarations
47
+ function_name = closure.name
48
+ check_type_match(parameters, arguments, function_name)
49
+
50
+ for parameter, argument in zip(parameters, arguments):
51
+ _update_scope(parameter, argument, closure)
52
+
53
+ parameter_names = {parameter.name for parameter in parameters}
54
+ for parameter in parameters:
55
+ if isinstance(parameter, QuantumOperandDeclaration):
56
+ parameter_value = closure.scope[parameter.name].value
57
+ _update_operand_signature_environment(
58
+ parameter_value, parameter_names, closure
59
+ )
60
+
61
+ return [
62
+ _evaluate_type_from_arg(
63
+ parameter, argument, Scope(parent=closure.scope | signature_scope)
64
+ )
65
+ for parameter, argument in zip(parameters, arguments)
66
+ ]
67
+
68
+
69
+ def _update_scope(
70
+ parameter: PositionalArg, argument: Evaluated, closure: FunctionClosure
71
+ ) -> None:
72
+ if not isinstance(parameter, PortDeclaration):
73
+ closure.scope[parameter.name] = argument
74
+ return
75
+ if parameter.direction is PortDeclarationDirection.Output:
76
+ return
77
+ quantum_symbol = argument.as_type(QuantumSymbol)
78
+ if not quantum_symbol.quantum_type.has_size_in_bits:
79
+ raise ClassiqExpansionError(
80
+ f"Argument {str(quantum_symbol.emit())!r} to function {closure.name!r} "
81
+ f"is not initialized"
82
+ )
83
+ casted_argument = _cast(
84
+ parameter.quantum_type,
85
+ quantum_symbol,
86
+ parameter.name,
87
+ )
88
+ closure.scope[parameter.name] = Evaluated(
89
+ value=casted_argument, defining_function=closure
90
+ )
91
+
92
+
93
+ NestedFunctionClosureT = Union[FunctionClosure, List["NestedFunctionClosureT"]]
94
+
95
+
96
+ def _update_operand_signature_environment(
97
+ operand_val: NestedFunctionClosureT,
98
+ parameter_names: Set[str],
99
+ closure: FunctionClosure,
100
+ ) -> None:
101
+ # We update the environment (parent) of the operand by adding closure.scope.data,
102
+ # which includes the parameters that appear in the function's signature only.
103
+ if isinstance(operand_val, list):
104
+ for operand in operand_val:
105
+ _update_operand_signature_environment(operand, parameter_names, closure)
106
+ return
107
+ if not isinstance(operand_val, FunctionClosure):
108
+ raise ClassiqInternalExpansionError
109
+ operand_val.signature_scope.update(
110
+ {
111
+ identifier: value
112
+ for identifier, value in closure.scope.data.items()
113
+ if identifier in parameter_names
114
+ }
115
+ )
116
+
117
+
118
+ def _cast(
119
+ quantum_type: QuantumType, quantum_symbol: QuantumSymbol, param_name: str
120
+ ) -> QuantumSymbol:
121
+ updated_quantum_type = quantum_type.copy()
122
+ _inject_quantum_arg_info_to_type(updated_quantum_type, quantum_symbol, param_name)
123
+ return QuantumSymbol(
124
+ handle=quantum_symbol.handle, quantum_type=updated_quantum_type
125
+ )
126
+
127
+
128
+ def _evaluate_type_from_arg(
129
+ parameter: PositionalArg, argument: Evaluated, inner_scope: Scope
130
+ ) -> PositionalArg:
131
+ if not isinstance(parameter, PortDeclaration):
132
+ return parameter
133
+
134
+ updated_quantum_type: QuantumType = evaluate_type_in_quantum_symbol(
135
+ parameter.quantum_type.copy(), inner_scope, parameter.name
136
+ )
137
+ if parameter.direction != PortDeclarationDirection.Output:
138
+ updated_quantum_type = _inject_quantum_arg_info_to_type(
139
+ updated_quantum_type, argument.as_type(QuantumSymbol), parameter.name
140
+ )
141
+ return parameter.copy(update={"quantum_type": updated_quantum_type})
142
+
143
+
144
+ def evaluate_type_in_quantum_symbol(
145
+ type_to_update: QuantumType, scope: Scope, param_name: str
146
+ ) -> ConcreteQuantumType:
147
+ if isinstance(type_to_update, QuantumBitvector):
148
+ return _evaluate_qarray_in_quantum_symbol(type_to_update, scope, param_name)
149
+ elif isinstance(type_to_update, QuantumNumeric):
150
+ return _evaluate_qnum_in_quantum_symbol(type_to_update, scope, param_name)
151
+ elif isinstance(type_to_update, TypeName):
152
+ return _evaluate_qstruct_in_quantum_symbol(type_to_update, scope, param_name)
153
+ else:
154
+ assert isinstance(type_to_update, QuantumBit)
155
+ return type_to_update
156
+
157
+
158
+ def _evaluate_qarray_in_quantum_symbol(
159
+ type_to_update: QuantumBitvector, scope: Scope, param_name: str
160
+ ) -> QuantumBitvector:
161
+ new_element_type = evaluate_type_in_quantum_symbol(
162
+ type_to_update.element_type, scope, param_name
163
+ )
164
+ set_element_type(type_to_update, new_element_type)
165
+ if type_to_update.length is not None:
166
+ new_length = evaluate_classical_expression(
167
+ type_to_update.length, scope
168
+ ).as_type(int)
169
+ set_length(type_to_update, new_length)
170
+ return type_to_update
171
+
172
+
173
+ def _evaluate_qnum_in_quantum_symbol(
174
+ type_to_update: QuantumNumeric, scope: Scope, param_name: str
175
+ ) -> QuantumNumeric:
176
+ if type_to_update.is_signed is not None:
177
+ new_is_sign = evaluate_classical_expression(
178
+ type_to_update.is_signed, scope
179
+ ).as_type(bool)
180
+ set_is_signed(type_to_update, new_is_sign, param_name)
181
+ if type_to_update.fraction_digits is not None:
182
+ new_fraction_digits = evaluate_classical_expression(
183
+ type_to_update.fraction_digits, scope
184
+ ).as_type(int)
185
+ set_fraction_digits(type_to_update, new_fraction_digits, param_name)
186
+ if type_to_update.size is not None:
187
+ new_size = evaluate_classical_expression(type_to_update.size, scope).as_type(
188
+ int
189
+ )
190
+ set_size(type_to_update, new_size, param_name)
191
+ return type_to_update
192
+
193
+
194
+ def _evaluate_qstruct_in_quantum_symbol(
195
+ type_to_update: TypeName, scope: Scope, param_name: str
196
+ ) -> TypeName:
197
+ new_fields = {
198
+ field_name: evaluate_type_in_quantum_symbol(field_type, scope, param_name)
199
+ for field_name, field_type in type_to_update.fields.items()
200
+ }
201
+ set_struct_fields(type_to_update, new_fields)
202
+ return type_to_update
203
+
204
+
205
+ def evaluate_types_in_quantum_symbols(
206
+ symbols: List[QuantumSymbol], scope: Scope
207
+ ) -> List[QuantumSymbol]:
208
+ return [
209
+ QuantumSymbol(
210
+ handle=symbol.handle,
211
+ quantum_type=evaluate_type_in_quantum_symbol(
212
+ symbol.quantum_type, scope, str(symbol.handle)
213
+ ),
214
+ )
215
+ for symbol in symbols
216
+ ]
217
+
218
+
219
+ def _inject_quantum_arg_info_to_type(
220
+ parameter_type: QuantumType, quantum_symbol: QuantumSymbol, param_name: str
221
+ ) -> QuantumType:
222
+ copy_type_information(
223
+ quantum_symbol.quantum_type,
224
+ parameter_type,
225
+ param_name,
226
+ )
227
+ return parameter_type