def parameterized_value_from_proto_dict(message: Dict) -> Union[ value.Symbol, float]: if 'raw' in message: return message['raw'] if 'parameter_key' in message: return value.Symbol(message['parameter_key']) raise ValueError('No value specified for parameterized float.')
def assert_has_consistent_apply_unitary_for_various_exponents( val: Any, *, exponents=(1, -0.5, 0.5, 0.25, -0.25, 0.1, value.Symbol('s')), qubit_count: Optional[int] = None) -> None: """Tests whether a value's _apply_unitary_to_tensor_ is correct. Contrasts the effects of the value's `_apply_unitary_to_tensor_` with the matrix returned by the value's `_unitary_` method. Attempts this after attempting to raise the value to several exponents. Args: val: The value under test. Should have a `__pow__` method. exponents: The exponents to try. Defaults to a variety of special and arbitrary angles, as well as a parameterized angle (a symbol). If the value's `__pow__` returns `NotImplemented` for any of these, they are skipped. qubit_count: A minimum qubit count for the test system. This argument isn't needed if the gate has a unitary matrix or implements `cirq.SingleQubitGate`/`cirq.TwoQubitGate`/`cirq.ThreeQubitGate`; it will be inferred. """ for exponent in exponents: gate = protocols.pow(val, exponent, default=None) if gate is not None: assert_has_consistent_apply_unitary( gate, qubit_count=qubit_count)
def assert_eigen_gate_has_consistent_apply_unitary( eigen_gate_type: Type[EigenGate], *, exponents=(1, -0.5, 0.5, 0.25, -0.25, 0.1, -1, value.Symbol('s')), global_shifts=(0, 0.5, -0.5), qubit_count: Optional[int] = None) -> None: """Tests whether an EigenGate type's _apply_unitary_to_tensor_ is correct. Contrasts the effects of the gate's `_apply_unitary_to_tensor_` with the matrix returned by the gate's `_unitary_` method, trying various values for the gate exponent and global shift. Args: eigen_gate_type: The type of gate to test. The type must have an __init__ method that takes an exponent and a global_shift. exponents: The exponents to try. Defaults to a variety of special and arbitrary angles, as well as a parameterized angle (a symbol). global_shifts: The global shifts to try. Defaults to a variety of special angles. qubit_count: The qubit count to use for the gate. This argument isn't needed if the gate has a unitary matrix or implements `cirq.SingleQubitGate`/`cirq.TwoQubitGate`/`cirq.ThreeQubitGate`; it will be inferred. """ for exponent in exponents: for shift in global_shifts: assert_has_consistent_apply_unitary( eigen_gate_type(exponent=exponent, global_shift=shift), qubit_count=qubit_count)
def _parameterized_value_from_proto_dict(message: Dict ) -> Union[value.Symbol, float]: if 'raw' in message: return message['raw'] if 'parameter_key' in message: return value.Symbol(message['parameter_key']) raise ValueError('No value specified for parameterized float. ' 'Expected "raw" or "parameter_key" to be set. ' 'message: {!r}'.format(message))
def parameterized_value_from_proto( message: operations_pb2.ParameterizedFloat ) -> Union[value.Symbol, float]: which = message.WhichOneof('value') if which == 'raw': return message.raw elif which == 'parameter_key': return value.Symbol(message.parameter_key) else: raise ValueError('No value specified for parameterized float.')
def assert_eigengate_implements_consistent_protocols( eigen_gate_type: Type[ops.EigenGate], *, exponents: Sequence[Union[value.Symbol, float]] = (0, 1, -1, 0.5, 0.25, -0.5, 0.1, value.Symbol('s')), global_shifts: Sequence[float] = (0, 0.5, -0.5, 0.1), qubit_count: Optional[int] = None, setup_code: str = 'import cirq\nimport numpy as np') -> None: """Checks that an EigenGate subclass is internally consistent and has a good __repr__.""" for exponent in exponents: for shift in global_shifts: _assert_meets_standards_helper( eigen_gate_type(exponent=exponent, global_shift=shift), qubit_count, setup_code)
def assert_implements_consistent_protocols( val: Any, *, exponents: Sequence[Any] = (0, 1, -1, 0.5, 0.25, -0.5, 0.1, value.Symbol('s')), qubit_count: Optional[int] = None, setup_code: str = 'import cirq\nimport numpy as np') -> None: """Checks that a value is internally consistent and has a good __repr__.""" _assert_meets_standards_helper(val, qubit_count, setup_code) for exponent in exponents: p = protocols.pow(val, exponent, None) if p is not None: _assert_meets_standards_helper(val**exponent, qubit_count, setup_code)
def assert_implements_consistent_protocols( val: Any, *, exponents: Sequence[Any] = (0, 1, -1, 0.5, 0.25, -0.5, 0.1, value.Symbol('s')), qubit_count: Optional[int] = None, ignoring_global_phase: bool = False, setup_code: str = 'import cirq\nimport numpy as np', global_vals: Optional[Dict[str, Any]] = None, local_vals: Optional[Dict[str, Any]] = None) -> None: """Checks that a value is internally consistent and has a good __repr__.""" global_vals = global_vals or {} local_vals = local_vals or {} _assert_meets_standards_helper(val, qubit_count, ignoring_global_phase, setup_code, global_vals, local_vals) for exponent in exponents: p = protocols.pow(val, exponent, None) if p is not None: _assert_meets_standards_helper(val**exponent, qubit_count, ignoring_global_phase, setup_code, global_vals, local_vals)