def test_thermal_relaxation_channel_repeated(backend): initial_state = random_state(5) c = Circuit(5) c.add( gates.ThermalRelaxationChannel(4, t1=1.0, t2=0.6, time=0.8, excited_population=0.8, seed=123)) final_state = c(K.cast(np.copy(initial_state)), nshots=30) pz, p0, p1 = c.queue[0].calculate_probabilities(1.0, 0.6, 0.8, 0.8) np.random.seed(123) target_state = [] collapse = gates.M(4, collapse=True) collapse.nqubits = 5 zgate, xgate = gates.Z(4), gates.X(4) for _ in range(30): state = K.cast(np.copy(initial_state)) if np.random.random() < pz: state = zgate(state) if np.random.random() < p0: state = K.state_vector_collapse(collapse, state, [0]) if np.random.random() < p1: state = K.state_vector_collapse(collapse, state, [0]) state = xgate(state) target_state.append(K.copy(state)) target_state = K.stack(target_state) K.assert_allclose(final_state, target_state)
def copy(self, deep=False): new = self.__class__(self.circuit) if deep: if self.pieces is not None: new.pieces = [K.copy(piece) for piece in self.pieces] new.measurements = copy.deepcopy(self.measurements) else: new.pieces = self.pieces new.measurements = self.measurements return new
def test_pauli_noise_channel(backend): initial_rho = random_density_matrix(2) gate = gates.PauliNoiseChannel(1, px=0.3) gate.density_matrix = True final_rho = gate(K.cast(np.copy(initial_rho))) gate = gates.X(1) gate.density_matrix = True initial_rho = K.cast(initial_rho) target_rho = 0.3 * gate(K.copy(initial_rho)) target_rho += 0.7 * initial_rho K.assert_allclose(final_rho, target_rho)
def apply_gates(self, state, density_matrix=False): """Applies gates corresponding to the Hamiltonian terms to a given state. Helper method for ``__matmul__``. """ total = 0 for term in self.terms: total += term(K.copy(state), density_matrix) if self.constant: total += self.constant * state return total
def copy(self, deep=False): new = self.__class__(self._nqubits) if deep: if self._tensor is not None: new.tensor = K.copy(self.tensor) new.measurements = copy.deepcopy(self.measurements) else: if self._tensor is not None: new.tensor = self.tensor new.measurements = self.measurements return new
def test_reset_channel_repeated(backend): initial_state = random_state(5) c = Circuit(5) c.add(gates.ResetChannel(2, p0=0.3, p1=0.3, seed=123)) final_state = c(K.cast(np.copy(initial_state)), nshots=30) np.random.seed(123) target_state = [] collapse = gates.M(2, collapse=True) collapse.nqubits = 5 xgate = gates.X(2) for _ in range(30): state = K.cast(np.copy(initial_state)) if np.random.random() < 0.3: state = K.state_vector_collapse(collapse, state, [0]) if np.random.random() < 0.3: state = K.state_vector_collapse(collapse, state, [0]) state = xgate(state) target_state.append(K.copy(state)) target_state = K.stack(target_state) K.assert_allclose(final_state, target_state)
def _repeated_execute(self, nreps, initial_state=None): results = [] initial_state = self.get_initial_state(initial_state) for _ in range(nreps): with self._device(): state = K.copy(initial_state) state = self._device_execute(state) if self.measurement_gate is None: results.append(state.tensor) else: state.measure(self.measurement_gate, nshots=1) results.append(state.measurements[0]) del (state) with self._device(): results = K.stack(results, axis=0) if self.measurement_gate is None: return results state = self.state_cls(self.nqubits) state.set_measurements(self.measurement_gate.qubits, results, self.measurement_tuples) return state
def construct_unitary(self): unitary = self.parameters rank = int(math.log2(int(unitary.shape[0]))) matrix = K.copy(K.cast(unitary)) return matrix
def _state_vector_call(self, state): if self.copy: return K.copy(state) else: return state
def construct_unitary(self): unitary = self.parameters if isinstance(unitary, K.qnp.Tensor): return K.qnp.cast(unitary) if isinstance(unitary, K.Tensor): # pragma: no cover return K.copy(K.cast(unitary))