Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _has_unitary_(self):
return protocols.has_unitary(self.to_circuit())
def _apply_unitary_(self, args: 'protocols.ApplyUnitaryArgs'
) -> Optional[np.ndarray]:
if protocols.is_parameterized(self):
return None
global_phase = 1j**(2 * self._exponent * self._global_shift)
if global_phase != 1:
args.target_tensor *= global_phase
relative_phase = 1j**(2 * self.exponent)
zo = args.subspace_index(0b01)
oz = args.subspace_index(0b10)
args.target_tensor[oz] *= relative_phase
args.target_tensor[zo] *= relative_phase
return args.target_tensor
def _json_dict_(self):
return protocols.obj_to_dict_helper(self, ['qubit_noise_gate'])
Raises:
ValueError if the qubits are not instances of Qid.
"""
for qubit in target:
if isinstance(qubit, np.ndarray):
raise ValueError(
'measure() was called a numpy ndarray. Perhaps you meant '
'to call measure_state_vector on numpy array?')
elif not isinstance(qubit, raw_types.Qid):
raise ValueError(
'measure() was called with type different than Qid.')
if key is None:
key = _default_measurement_key(target)
qid_shape = protocols.qid_shape(target)
return MeasurementGate(len(target), key, invert_mask, qid_shape).on(*target)
def _pauli_string_diagram_info(
self,
args: 'protocols.CircuitDiagramInfoArgs',
exponent: Any = 1,
) -> 'protocols.CircuitDiagramInfo':
qubits = self.qubits if args.known_qubits is None else args.known_qubits
syms = tuple(
'[{}]'.format(self.pauli_string[qubit]) for qubit in qubits)
return protocols.CircuitDiagramInfo(wire_symbols=syms,
exponent=exponent)
is_blocker=lambda next_op: len(
next_op.qubits) != 1 or not protocols.has_unitary(next_op))
operations = [op for idx,op in op_list]
def _circuit_diagram_info_(self, args: 'protocols.CircuitDiagramInfoArgs'
) -> 'protocols.CircuitDiagramInfo':
return protocols.CircuitDiagramInfo(
wire_symbols=('YY', 'YY'),
exponent=self._diagram_exponent(args))
),
],
),
]
#
# Measurement Serializer and Deserializer
#
MEASUREMENT_SERIALIZER = op_serializer.GateOpSerializer(
gate_type=ops.MeasurementGate,
serialized_gate_id='meas',
args=[
op_serializer.SerializingArg(serialized_name='key',
serialized_type=str,
gate_getter=protocols.measurement_key),
op_serializer.SerializingArg(serialized_name='invert_mask',
serialized_type=List[bool],
gate_getter='invert_mask'),
])
MEASUREMENT_DESERIALIZER = op_deserializer.GateOpDeserializer(
serialized_gate_id='meas',
gate_constructor=ops.MeasurementGate,
args=[
op_deserializer.DeserializingArg(serialized_name='key',
constructor_arg_name='key'),
op_deserializer.DeserializingArg(
serialized_name='invert_mask',
constructor_arg_name='invert_mask',
value_func=lambda x: tuple(cast(list, x)))
],
num_qubits_param='num_qubits')
apply to. An operation targeting the k'th qubit in this list will
operate on the k'th axis of the state tensor.
dtype: The numpy dtype to use for applying the unitary. Must be a
complex dtype.
Returns:
The left-multiplied state tensor.
"""
buffer = np.empty_like(state)
def on_stuck(bad_op):
return TypeError(
'Operation without a known matrix or decomposition: {!r}'.format(
bad_op))
unitary_ops = protocols.decompose(
circuit.all_operations(),
keep=protocols.has_unitary,
intercepting_decomposer=_decompose_measurement_inversions,
on_stuck_raise=on_stuck)
return protocols.apply_unitaries(
unitary_ops, qubits,
protocols.ApplyUnitaryArgs(state, buffer, range(len(qubits))))