|
| 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 | +from typing import Any, Dict |
| 16 | + |
| 17 | +from cirq import value |
| 18 | + |
| 19 | + |
| 20 | +@value.value_equality |
| 21 | +class InternalTag: |
| 22 | + """InternalTag is a placeholder tag for internal tags that |
| 23 | + are not specified publicly. |
| 24 | +
|
| 25 | + These tags can hold the information needed to instantiate a |
| 26 | + tag specified in an internal library. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, name: str, package: str, **kwargs): |
| 30 | + """Instantiates an InternalTag. |
| 31 | +
|
| 32 | + Arguments: |
| 33 | + name: Tag class name. |
| 34 | + package: The python module of the tag. |
| 35 | + **kwargs: Arbitrary keyword parameters that should be passed to the tag. |
| 36 | + """ |
| 37 | + self.name = name |
| 38 | + self.package = package |
| 39 | + self.tag_args = kwargs |
| 40 | + |
| 41 | + def __str__(self): |
| 42 | + tag_args = ', '.join(f'{k}={v}' for k, v in self.tag_args.items()) |
| 43 | + return f'{self.package}.{self.name}({tag_args})' |
| 44 | + |
| 45 | + def __repr__(self) -> str: |
| 46 | + tag_args = ', '.join(f'{k}={v!r}' for k, v in self.tag_args.items()) |
| 47 | + if tag_args: |
| 48 | + tag_args = ', ' + tag_args |
| 49 | + return f"cirq_google.InternalTag(name='{self.name}', package='{self.package}'{tag_args})" |
| 50 | + |
| 51 | + def _json_dict_(self) -> Dict[str, Any]: |
| 52 | + return dict(name=self.name, package=self.package, **self.tag_args) |
| 53 | + |
| 54 | + def _value_equality_values_(self): |
| 55 | + try: |
| 56 | + tag_args_eq_values = frozenset(self.tag_args.items()) |
| 57 | + except TypeError: |
| 58 | + tag_args_eq_values = self.tag_args |
| 59 | + return (self.name, self.package, tag_args_eq_values) |
0 commit comments