def _create_partial_act_on_args( self, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE', 'cirq.ActOnDensityMatrixArgs'], qubits: Sequence['cirq.Qid'], classical_data: 'cirq.ClassicalDataStore', ) -> 'cirq.ActOnDensityMatrixArgs': """Creates the ActOnDensityMatrixArgs for a circuit. Args: initial_state: The initial state for the simulation in the computational basis. qubits: Determines the canonical ordering of the qubits. This is often used in specifying the initial state, i.e. the ordering of the computational basis states. classical_data: The shared classical data container for this simulation. Returns: ActOnDensityMatrixArgs for the circuit. """ if isinstance(initial_state, act_on_density_matrix_args.ActOnDensityMatrixArgs): return initial_state if self._ignore_measurement_results: return act_on_density_matrix_args.ActOnDensityMatrixArgs( qubits=qubits, prng=self._prng, classical_data=classical_data, ignore_measurement_results=self._ignore_measurement_results, initial_state=initial_state, dtype=self._dtype, ) return act_on_density_matrix_args.ActOnDensityMatrixArgs( qubits=qubits, prng=self._prng, classical_data=classical_data, initial_state=initial_state, dtype=self._dtype, )
def _create_partial_act_on_args( self, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE', 'cirq.ActOnDensityMatrixArgs'], qubits: Sequence['cirq.Qid'], logs: Dict[str, Any], ) -> 'cirq.ActOnDensityMatrixArgs': """Creates the ActOnDensityMatrixArgs for a circuit. Args: initial_state: The initial state for the simulation in the computational basis. qubits: Determines the canonical ordering of the qubits. This is often used in specifying the initial state, i.e. the ordering of the computational basis states. logs: The log of measurement results that is added into. Returns: ActOnDensityMatrixArgs for the circuit. """ if isinstance(initial_state, act_on_density_matrix_args.ActOnDensityMatrixArgs): return initial_state qid_shape = protocols.qid_shape(qubits) initial_matrix = qis.to_valid_density_matrix(initial_state, len(qid_shape), qid_shape=qid_shape, dtype=self._dtype) if np.may_share_memory(initial_matrix, initial_state): initial_matrix = initial_matrix.copy() tensor = initial_matrix.reshape(qid_shape * 2) return act_on_density_matrix_args.ActOnDensityMatrixArgs( target_tensor=tensor, available_buffer=[np.empty_like(tensor) for _ in range(3)], qubits=qubits, qid_shape=qid_shape, prng=self._prng, log_of_measurement_results=logs, )
def _base_iterator( self, circuit: circuits.Circuit, qubit_order: ops.QubitOrderOrList, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'], all_measurements_are_terminal=False, is_raw_state=False, ) -> Iterator['DensityMatrixStepResult']: qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for( circuit.all_qubits()) qid_shape = protocols.qid_shape(qubits) qubit_map = {q: i for i, q in enumerate(qubits)} initial_matrix = (qis.to_valid_density_matrix(initial_state, len(qid_shape), qid_shape=qid_shape, dtype=self._dtype) if not is_raw_state else initial_state) if np.may_share_memory(initial_matrix, initial_state): initial_matrix = initial_matrix.copy() if len(circuit) == 0: yield DensityMatrixStepResult(initial_matrix, {}, qubit_map, self._dtype) return tensor = initial_matrix.reshape(qid_shape * 2) sim_state = act_on_density_matrix_args.ActOnDensityMatrixArgs( target_tensor=tensor, available_buffer=[np.empty_like(tensor) for _ in range(3)], axes=[], qid_shape=qid_shape, prng=self._prng, log_of_measurement_results={}, ) noisy_moments = self.noise.noisy_moments(circuit, sorted(circuit.all_qubits())) measured = collections.defaultdict( bool) # type: Dict[Tuple[cirq.Qid, ...], bool] for moment in noisy_moments: for op in flatten_to_ops(moment): # TODO: support more general measurements. # Github issue: https://github.com/quantumlib/Cirq/issues/3566 if all_measurements_are_terminal and measured[op.qubits]: continue if protocols.is_measurement(op): measured[op.qubits] = True if all_measurements_are_terminal: continue if self._ignore_measurement_results: op = ops.phase_damp(1).on(*op.qubits) sim_state.axes = tuple(qubit_map[qubit] for qubit in op.qubits) protocols.act_on(op, sim_state) yield DensityMatrixStepResult( density_matrix=sim_state.target_tensor, measurements=dict(sim_state.log_of_measurement_results), qubit_map=qubit_map, dtype=self._dtype, ) sim_state.log_of_measurement_results.clear()