Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
density_matrix.h
Go to the documentation of this file.
1 /*
2 Copyright 2025 SQUANDER Contributors
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 
17 #pragma once
18 
19 #include "matrix_base.hpp" // From existing SQUANDER
20 #include <vector>
21 
22 namespace squander {
23 namespace density {
24 
53 class DensityMatrix : public matrix_base<QGD_Complex16> {
54 public:
55  // ================================================================
56  // Constructors & Destructor
57  // ================================================================
58 
67  explicit DensityMatrix(int qbit_num);
68 
78  explicit DensityMatrix(const matrix_base<QGD_Complex16> &state_vector);
79 
89  DensityMatrix(QGD_Complex16 *data, int dim);
90 
97  DensityMatrix(const DensityMatrix &other);
98 
105  DensityMatrix(DensityMatrix &&other) noexcept;
106 
110  ~DensityMatrix() = default;
111 
112  // ================================================================
113  // Assignment Operators
114  // ================================================================
115 
116  DensityMatrix &operator=(const DensityMatrix &other);
117  DensityMatrix &operator=(DensityMatrix &&other) noexcept;
118 
126  QGD_Complex16 &operator()(int i, int j);
127  const QGD_Complex16 &operator()(int i, int j) const;
128 
129  // ================================================================
130  // Basic Properties
131  // ================================================================
132 
137  int get_qbit_num() const { return qbit_num_; }
138 
143  int get_dim() const { return rows; }
144 
145  // ================================================================
146  // Quantum Properties
147  // ================================================================
148 
156  QGD_Complex16 trace() const;
157 
166  double purity() const;
167 
176  double entropy() const;
177 
188  bool is_valid(double tol = 1e-10) const;
189 
197  std::vector<double> eigenvalues() const;
198 
199  // ================================================================
200  // Operations
201  // ================================================================
202 
216 
229  int target_qbit);
230 
243  int target_qbit, int control_qbit);
244 
255  void apply_local_unitary(const matrix_base<QGD_Complex16> &u_kernel,
256  const std::vector<int> &target_qbits);
257 
268  DensityMatrix partial_trace(const std::vector<int> &trace_out) const;
269 
277  DensityMatrix clone() const;
278 
279  // ================================================================
280  // Static Factory Methods
281  // ================================================================
282 
293  static DensityMatrix maximally_mixed(int qbit_num);
294 
295  // ================================================================
296  // Utilities
297  // ================================================================
298 
310  void print() const;
311 
312 private:
313  int qbit_num_;
314 
315  // Helper methods
316  void validate_dimensions() const;
317  bool is_hermitian(double tol) const;
318 };
319 
320 } // namespace density
321 } // namespace squander
void apply_local_unitary(const matrix_base< QGD_Complex16 > &u_kernel, const std::vector< int > &target_qbits)
Apply k-qubit unitary using local kernel (general case)
QGD_Complex16 * data
pointer to the stored data
Definition: matrix_base.hpp:48
DensityMatrix(int qbit_num)
Create density matrix for n qubits.
~DensityMatrix()=default
Destructor (uses base class default)
DensityMatrix clone() const
Create deep copy.
bool is_valid(double tol=1e-10) const
Check if valid density matrix.
double entropy() const
von Neumann entropy: S(ρ) = -Tr(ρ log₂ ρ)
double purity() const
Calculate purity: Tr(ρ²)
Base Class to store data of arrays and its properties.
Definition: matrix_base.hpp:38
DensityMatrix partial_trace(const std::vector< int > &trace_out) const
Compute partial trace over specified qubits.
int rows
The number of rows.
Definition: matrix_base.hpp:42
DensityMatrix & operator=(const DensityMatrix &other)
std::vector< double > eigenvalues() const
Get eigenvalues (sorted descending)
bool is_hermitian(double tol) const
Quantum density matrix ρ for mixed-state representation.
QGD_Complex16 trace() const
Calculate trace: Tr(ρ)
void apply_unitary(const matrix_base< QGD_Complex16 > &U)
Apply unitary transformation: ρ → UρU†
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
void print() const
Print matrix with properties.
int get_qbit_num() const
Get number of qubits.
QGD_Complex16 & operator()(int i, int j)
Element access: ρ(i,j)
int qbit_num_
Number of qubits.
int get_dim() const
Get matrix dimension (2^qbit_num)
void apply_single_qubit_unitary(const matrix_base< QGD_Complex16 > &u_2x2, int target_qbit)
Apply single-qubit unitary using local kernel (optimized)
void apply_two_qubit_unitary(const matrix_base< QGD_Complex16 > &u_2x2, int target_qbit, int control_qbit)
Apply two-qubit controlled unitary using local kernel (optimized)
static DensityMatrix maximally_mixed(int qbit_num)
Create maximally mixed state: ρ = I/2^n.