|
| 1 | +# Copyright 2025 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +import sympy |
| 17 | +import tunits as tu |
| 18 | + |
| 19 | +import cirq |
| 20 | +from cirq_google.study import symbol_util as su |
| 21 | + |
| 22 | + |
| 23 | +def test_dict_param_name(): |
| 24 | + d = {"a": 54, "b": sympy.Symbol("t"), "c": sympy.Symbol("t"), "d": "sd"} |
| 25 | + |
| 26 | + assert su.dict_param_name(None) == set() |
| 27 | + assert su.dict_param_name(d) == {"t"} |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "d,expected", |
| 32 | + [ |
| 33 | + (None, False), |
| 34 | + ({}, False), |
| 35 | + ({"a": 50}, False), |
| 36 | + ({"a": 54, "b": sympy.Symbol("t"), "c": sympy.Symbol("t"), "d": "sd"}, True), |
| 37 | + ], |
| 38 | +) |
| 39 | +def test_is_parameterized_dict(d, expected): |
| 40 | + assert su.is_parameterized_dict(d) == expected |
| 41 | + |
| 42 | + |
| 43 | +def test_direct_symbol_replacement(): |
| 44 | + value_list = [sympy.Symbol("t"), sympy.Symbol("v"), sympy.Symbol("z"), 123, "fd"] |
| 45 | + resolver = cirq.ParamResolver({"t": 5 * tu.ns, sympy.Symbol("v"): 8 * tu.GHz}) |
| 46 | + value_resolved = [su.direct_symbol_replacement(v, resolver) for v in value_list] |
| 47 | + |
| 48 | + assert value_resolved == [5 * tu.ns, 8 * tu.GHz, sympy.Symbol("z"), 123, "fd"] |
0 commit comments