You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.cc 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  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. #include "mindquantum/utils.h"
  17. namespace mindspore {
  18. namespace mindquantum {
  19. ComplexType ComplexInnerProduct(const Simulator::StateVector &v1, const Simulator::StateVector &v2, unsigned len) {
  20. CalcType real_part = 0;
  21. CalcType imag_part = 0;
  22. #pragma omp parallel for reduction(+ : real_part, imag_part)
  23. for (Index i = 0; i < len; i++) {
  24. real_part += v1[i].real() * v2[i].real() + v1[i].imag() * v2[i].imag();
  25. imag_part += v1[i].real() * v2[i].imag() - v1[i].imag() * v2[i].real();
  26. }
  27. ComplexType result = {real_part, imag_part};
  28. return result;
  29. }
  30. ComplexType ComplexInnerProductWithControl(const Simulator::StateVector &v1, const Simulator::StateVector &v2,
  31. Index len, std::size_t ctrlmask) {
  32. CalcType real_part = 0;
  33. CalcType imag_part = 0;
  34. #pragma omp parallel for reduction(+ : real_part, imag_part)
  35. for (std::size_t i = 0; i < len; i++) {
  36. if ((i & ctrlmask) == ctrlmask) {
  37. real_part += v1[i].real() * v2[i].real() + v1[i].imag() * v2[i].imag();
  38. imag_part += v1[i].real() * v2[i].imag() - v1[i].imag() * v2[i].real();
  39. }
  40. }
  41. ComplexType result = {real_part, imag_part};
  42. return result;
  43. }
  44. } // namespace mindquantum
  45. } // namespace mindspore