Exemple #1
0
    def _create_simulation_state(
            self, initial_state: Any, qubits: Sequence['cirq.Qid']
    ) -> SimulationStateBase[TSimulationState]:
        if isinstance(initial_state, SimulationStateBase):
            return initial_state

        classical_data = value.ClassicalDataDictionaryStore()
        if self._split_untangled_states:
            args_map: Dict[Optional['cirq.Qid'], TSimulationState] = {}
            if isinstance(initial_state, int):
                for q in reversed(qubits):
                    args_map[q] = self._create_partial_simulation_state(
                        initial_state=initial_state % q.dimension,
                        qubits=[q],
                        classical_data=classical_data,
                    )
                    initial_state = int(initial_state / q.dimension)
            else:
                args = self._create_partial_simulation_state(
                    initial_state=initial_state,
                    qubits=qubits,
                    classical_data=classical_data)
                for q in qubits:
                    args_map[q] = args
            args_map[None] = self._create_partial_simulation_state(
                0, (), classical_data)
            return SimulationProductState(args_map,
                                          qubits,
                                          self._split_untangled_states,
                                          classical_data=classical_data)
        else:
            return self._create_partial_simulation_state(
                initial_state=initial_state,
                qubits=qubits,
                classical_data=classical_data)
Exemple #2
0
    def apply_measurement(
        self,
        op: 'cirq.Operation',
        measurements: Dict[str, List[np.ndarray]],
        prng: np.random.RandomState,
        collapse_state_vector=True,
    ):
        if not isinstance(op.gate, cirq.MeasurementGate):
            raise TypeError(
                'apply_measurement only supports cirq.MeasurementGate operations. Found %s instead.'
                % str(op.gate))

        if collapse_state_vector:
            state = self
        else:
            state = self.copy()

        classical_data = value.ClassicalDataDictionaryStore()
        ch_form_args = clifford.ActOnStabilizerCHFormArgs(
            prng=prng,
            classical_data=classical_data,
            qubits=self.qubit_map.keys(),
            initial_state=state.ch_form,
        )
        act_on(op, ch_form_args)
        measurements.update(
            {str(k): list(v[-1])
             for k, v in classical_data.records.items()})
Exemple #3
0
    def _create_act_on_args(
        self,
        initial_state: Any,
        qubits: Sequence['cirq.Qid'],
    ) -> OperationTarget[TActOnArgs]:
        if isinstance(initial_state, OperationTarget):
            return initial_state

        classical_data = value.ClassicalDataDictionaryStore()
        if self._split_untangled_states:
            args_map: Dict[Optional['cirq.Qid'], TActOnArgs] = {}
            if isinstance(initial_state, int):
                for q in reversed(qubits):
                    args_map[q] = self._create_partial_act_on_args(
                        initial_state=initial_state % q.dimension,
                        qubits=[q],
                        classical_data=classical_data,
                    )
                    initial_state = int(initial_state / q.dimension)
            else:
                args = self._create_partial_act_on_args(
                    initial_state=initial_state,
                    qubits=qubits,
                    classical_data=classical_data,
                )
                for q in qubits:
                    args_map[q] = args
            args_map[None] = self._create_partial_act_on_args(
                0, (), classical_data)
            return ActOnArgsContainer(args_map,
                                      qubits,
                                      self._split_untangled_states,
                                      classical_data=classical_data)
        else:
            return self._create_partial_act_on_args(
                initial_state=initial_state,
                qubits=qubits,
                classical_data=classical_data,
            )
Exemple #4
0
    def __init__(
        self,
        prng: Optional[np.random.RandomState] = None,
        qubits: Optional[Sequence['cirq.Qid']] = None,
        log_of_measurement_results: Optional[Dict[str, List[int]]] = None,
        ignore_measurement_results: bool = False,
        classical_data: Optional['cirq.ClassicalDataStore'] = None,
    ):
        """Inits ActOnArgs.

        Args:
            prng: The pseudo random number generator to use for probabilistic
                effects.
            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.
            log_of_measurement_results: A mutable object that measurements are
                being recorded into.
            ignore_measurement_results: If True, then the simulation
                will treat measurement as dephasing instead of collapsing
                process, and not log the result. This is only applicable to
                simulators that can represent mixed states.
            classical_data: The shared classical data container for this
                simulation.
        """
        if prng is None:
            prng = cast(np.random.RandomState, np.random)
        if qubits is None:
            qubits = ()
        self._set_qubits(qubits)
        self._prng = prng
        self._classical_data = classical_data or value.ClassicalDataDictionaryStore(
            _records={
                value.MeasurementKey.parse_serialized(k): [tuple(v)]
                for k, v in (log_of_measurement_results or {}).items()
            }
        )
        self._ignore_measurement_results = ignore_measurement_results
    def __init__(
        self,
        prng: Optional[np.random.RandomState] = None,
        qubits: Optional[Sequence['cirq.Qid']] = None,
        log_of_measurement_results: Optional[Dict[str, List[int]]] = None,
        classical_data: Optional['cirq.ClassicalDataStore'] = None,
        state: Optional[TState] = None,
    ):
        """Inits SimulationState.

        Args:
            prng: The pseudo random number generator to use for probabilistic
                effects.
            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.
            log_of_measurement_results: A mutable object that measurements are
                being recorded into.
            classical_data: The shared classical data container for this
                simulation.
            state: The underlying quantum state of the simulation.
        """
        if qubits is None:
            qubits = ()
        classical_data = classical_data or value.ClassicalDataDictionaryStore(
            _records={
                value.MeasurementKey.parse_serialized(k): [tuple(v)]
                for k, v in (log_of_measurement_results or {}).items()
            })
        super().__init__(qubits=qubits, classical_data=classical_data)
        if prng is None:
            prng = cast(np.random.RandomState, np.random)
        self._prng = prng
        self._state = cast(TState, state)
        if state is None:
            _warn_or_error(
                'This function will require a valid `state` input in cirq v0.16.'
            )
    def __init__(
        self,
        sim_states: Dict[Optional['cirq.Qid'], TSimulationState],
        qubits: Sequence['cirq.Qid'],
        split_untangled_states: bool,
        classical_data: Optional['cirq.ClassicalDataStore'] = None,
    ):
        """Initializes the class.

        Args:
            sim_states: The `SimulationState` dictionary. This will not be
                copied; the original reference will be kept here.
            qubits: The canonical ordering of qubits.
            split_untangled_states: If True, optimizes operations by running
                unentangled qubit sets independently and merging those states
                at the end.
            classical_data: The shared classical data container for this
                simulation.
        """
        classical_data = classical_data or value.ClassicalDataDictionaryStore()
        super().__init__(qubits=qubits, classical_data=classical_data)
        self._sim_states = sim_states
        self._split_untangled_states = split_untangled_states