Skip to content

Commit

Permalink
Fix for #2286
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanro committed Jan 25, 2025
1 parent 0bd1716 commit e229528
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
prelude: >
MPS simulator had global bond dimension and singular values truncation threshold
fixes:
- |
Each MPS simulator has now its own settings for the bond dimension and singular values
truncation threshold. Refer to `#2286 <https://github.com/Qiskit/qiskit-aer/issues/2286>`
for more information.
10 changes: 5 additions & 5 deletions src/simulators/matrix_product_state/matrix_product_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ size_t State::required_memory_mb(uint_t num_qubits,

void State::set_config(const Config &config) {
// Set threshold for truncating Schmidt coefficients
MPS_Tensor::set_truncation_threshold(
qreg_.set_truncation_threshold(
config.matrix_product_state_truncation_threshold);

if (config.matrix_product_state_max_bond_dimension.has_value())
MPS_Tensor::set_max_bond_dimension(
qreg_.set_max_bond_dimension(
config.matrix_product_state_max_bond_dimension.value());
else
MPS_Tensor::set_max_bond_dimension(UINT64_MAX);
qreg_.set_max_bond_dimension(UINT64_MAX);

// Set threshold for truncating snapshots
MPS::set_json_chop_threshold(config.chop_threshold);
Expand Down Expand Up @@ -363,9 +363,9 @@ void State::set_config(const Config &config) {
}

void State::add_metadata(ExperimentResult &result) const {
result.metadata.add(MPS_Tensor::get_truncation_threshold(),
result.metadata.add(qreg_.get_truncation_threshold(),
"matrix_product_state_truncation_threshold");
result.metadata.add(MPS_Tensor::get_max_bond_dimension(),
result.metadata.add(qreg_.get_max_bond_dimension(),
"matrix_product_state_max_bond_dimension");
result.metadata.add(MPS::get_sample_measure_alg(),
"matrix_product_state_sample_measure_algorithm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,9 @@ void MPS::common_apply_2_qubit_gate(

MPS_Tensor left_gamma, right_gamma;
rvector_t lambda;
double discarded_value = MPS_Tensor::Decompose(temp, left_gamma, lambda,
right_gamma, MPS::mps_lapack_);
double discarded_value = MPS_Tensor::Decompose(
temp, left_gamma, lambda, right_gamma, MPS::mps_lapack_,
max_bond_dimension_, truncation_threshold_);

if (discarded_value > json_chop_threshold_)
MPS::print_to_log("discarded_value=", discarded_value, ", ");
Expand Down Expand Up @@ -1804,8 +1805,8 @@ void MPS::initialize_from_matrix(uint_t num_qubits, const cmatrix_t &mat) {
S.clear();
S.resize(std::min(reshaped_matrix.GetRows(), reshaped_matrix.GetColumns()));
csvd_wrapper(reshaped_matrix, U, S, V, MPS::mps_lapack_);
reduce_zeros(U, S, V, MPS_Tensor::get_max_bond_dimension(),
MPS_Tensor::get_truncation_threshold(), MPS::mps_lapack_);
reduce_zeros(U, S, V, get_max_bond_dimension(), get_truncation_threshold(),
MPS::mps_lapack_);

// step 3 - update q_reg_ with new gamma and new lambda
// increment number of qubits in the MPS structure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ class MPS {
reg_t &index_vec,
const reg_t &qubits) const;

void set_max_bond_dimension(uint_t max_bond_dimension) {
max_bond_dimension_ = max_bond_dimension;
}
void set_truncation_threshold(double truncation_threshold) {
truncation_threshold_ = truncation_threshold;
}

static void set_omp_threads(uint_t threads) {
if (threads > 0)
omp_threads_ = threads;
Expand Down Expand Up @@ -322,6 +329,9 @@ class MPS {

static void set_mps_lapack_svd(bool mps_lapack) { mps_lapack_ = mps_lapack; }

uint_t get_max_bond_dimension() const { return max_bond_dimension_; }
double get_truncation_threshold() const { return truncation_threshold_; }

static uint_t get_omp_threads() { return omp_threads_; }
static uint_t get_omp_threshold() { return omp_threshold_; }
static double get_json_chop_threshold() { return json_chop_threshold_; }
Expand Down Expand Up @@ -557,6 +567,9 @@ class MPS {
//-----------------------------------------------------------------------
// Config settings
//-----------------------------------------------------------------------
uint_t max_bond_dimension_ = UINT64_MAX;
double truncation_threshold_ = 1e-16;

static uint_t omp_threads_; // Disable multithreading by default
static uint_t
omp_threshold_; // Qubit threshold for multithreading when enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,8 @@ class MPS_Tensor {
chop_threshold_ = chop_threshold;
}

static void set_max_bond_dimension(uint_t max_bond_dimension) {
max_bond_dimension_ = max_bond_dimension;
}

static void set_truncation_threshold(double truncation_threshold) {
truncation_threshold_ = truncation_threshold;
}

static double get_chop_threshold() { return chop_threshold_; }

static uint_t get_max_bond_dimension() { return max_bond_dimension_; }

static double get_truncation_threshold() { return truncation_threshold_; }
//------------------------------------------------------------------
// function name: get_dim
// Description: Get the dimension of the physical index of the tensor
Expand Down Expand Up @@ -159,7 +148,8 @@ class MPS_Tensor {
const MPS_Tensor &right_gamma, bool mul_by_lambda);
static double Decompose(MPS_Tensor &temp, MPS_Tensor &left_gamma,
rvector_t &lambda, MPS_Tensor &right_gamma,
bool mps_lapack);
bool mps_lapack, uint_t max_bond_dimension,
double truncation_threshold);
static void
reshape_for_3_qubits_before_SVD(const std::vector<cmatrix_t> &data,
MPS_Tensor &reshaped_tensor);
Expand All @@ -179,16 +169,12 @@ class MPS_Tensor {
std::vector<cmatrix_t> data_;

static double chop_threshold_;
static uint_t max_bond_dimension_;
static double truncation_threshold_;
};

//=========================================================================
// Implementation
//=========================================================================
double MPS_Tensor::chop_threshold_ = CHOP_THRESHOLD;
uint_t MPS_Tensor::max_bond_dimension_ = UINT64_MAX;
double MPS_Tensor::truncation_threshold_ = 1e-16;

const double MPS_Tensor::SQR_HALF = sqrt(0.5);

Expand Down Expand Up @@ -593,16 +579,17 @@ void MPS_Tensor::contract_2_dimensions(const MPS_Tensor &left_gamma,
//---------------------------------------------------------------
double MPS_Tensor::Decompose(MPS_Tensor &temp, MPS_Tensor &left_gamma,
rvector_t &lambda, MPS_Tensor &right_gamma,
bool mps_lapack) {
bool mps_lapack, uint_t max_bond_dimension,
double truncation_threshold) {
cmatrix_t C;
C = reshape_before_SVD(temp.data_);
cmatrix_t U, V;
rvector_t S(std::min(C.GetRows(), C.GetColumns()));

csvd_wrapper(C, U, S, V, mps_lapack);
double discarded_value = 0.0;
discarded_value = reduce_zeros(U, S, V, max_bond_dimension_,
truncation_threshold_, mps_lapack);
discarded_value = reduce_zeros(U, S, V, max_bond_dimension,
truncation_threshold, mps_lapack);

left_gamma.data_ = reshape_U_after_SVD(U);
lambda = S;
Expand Down

0 comments on commit e229528

Please sign in to comment.