From e229528b6083072e544f5badff1b8b88fd0f4daa Mon Sep 17 00:00:00 2001 From: Adrian Roman Date: Sat, 25 Jan 2025 14:42:01 +0200 Subject: [PATCH] Fix for https://github.com/Qiskit/qiskit-aer/issues/2286 --- ...truncation-threshold-47dacf39fdee5c28.yaml | 8 ++++++ .../matrix_product_state.hpp | 10 ++++---- .../matrix_product_state_internal.cpp | 9 ++++--- .../matrix_product_state_internal.hpp | 13 ++++++++++ .../matrix_product_state_tensor.hpp | 25 +++++-------------- 5 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 releasenotes/notes/mps-sim-has-global-bond-dim-and-truncation-threshold-47dacf39fdee5c28.yaml diff --git a/releasenotes/notes/mps-sim-has-global-bond-dim-and-truncation-threshold-47dacf39fdee5c28.yaml b/releasenotes/notes/mps-sim-has-global-bond-dim-and-truncation-threshold-47dacf39fdee5c28.yaml new file mode 100644 index 0000000000..a2f1c55851 --- /dev/null +++ b/releasenotes/notes/mps-sim-has-global-bond-dim-and-truncation-threshold-47dacf39fdee5c28.yaml @@ -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 ` + for more information. diff --git a/src/simulators/matrix_product_state/matrix_product_state.hpp b/src/simulators/matrix_product_state/matrix_product_state.hpp index d4b540d6a1..5aa44c2502 100644 --- a/src/simulators/matrix_product_state/matrix_product_state.hpp +++ b/src/simulators/matrix_product_state/matrix_product_state.hpp @@ -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); @@ -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"); diff --git a/src/simulators/matrix_product_state/matrix_product_state_internal.cpp b/src/simulators/matrix_product_state/matrix_product_state_internal.cpp index 424637c4f0..1b73dbd346 100644 --- a/src/simulators/matrix_product_state/matrix_product_state_internal.cpp +++ b/src/simulators/matrix_product_state/matrix_product_state_internal.cpp @@ -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, ", "); @@ -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 diff --git a/src/simulators/matrix_product_state/matrix_product_state_internal.hpp b/src/simulators/matrix_product_state/matrix_product_state_internal.hpp index 1180e6cddf..d5825d2933 100644 --- a/src/simulators/matrix_product_state/matrix_product_state_internal.hpp +++ b/src/simulators/matrix_product_state/matrix_product_state_internal.hpp @@ -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; @@ -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_; } @@ -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 diff --git a/src/simulators/matrix_product_state/matrix_product_state_tensor.hpp b/src/simulators/matrix_product_state/matrix_product_state_tensor.hpp index cc090deb64..6de1169efe 100644 --- a/src/simulators/matrix_product_state/matrix_product_state_tensor.hpp +++ b/src/simulators/matrix_product_state/matrix_product_state_tensor.hpp @@ -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 @@ -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 &data, MPS_Tensor &reshaped_tensor); @@ -179,16 +169,12 @@ class MPS_Tensor { std::vector 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); @@ -593,7 +579,8 @@ 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; @@ -601,8 +588,8 @@ double MPS_Tensor::Decompose(MPS_Tensor &temp, MPS_Tensor &left_gamma, 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;