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.

sparse.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/sparse.h"
  17. namespace mindspore {
  18. namespace mindquantum {
  19. namespace sparse {
  20. SparseMatrix BasiGateSparse(char g) {
  21. SparseMatrix out(2, 2);
  22. out.reserve(VectorXi::Constant(2, 2));
  23. switch (g) {
  24. case 'X':
  25. case 'x':
  26. out.insert(0, 1) = 1;
  27. out.insert(1, 0) = 1;
  28. break;
  29. case 'Y':
  30. case 'y':
  31. out.insert(0, 1) = {0, -1};
  32. out.insert(1, 0) = {0, 1};
  33. break;
  34. case 'Z':
  35. case 'z':
  36. out.insert(0, 0) = 1;
  37. out.insert(1, 1) = -1;
  38. break;
  39. case '0':
  40. out.insert(0, 0) = 1;
  41. break;
  42. case '1':
  43. out.insert(1, 1) = 1;
  44. break;
  45. default:
  46. out.insert(0, 0) = 1;
  47. out.insert(1, 1) = 1;
  48. break;
  49. }
  50. out.makeCompressed();
  51. return out;
  52. }
  53. SparseMatrix IdentitySparse(int n_qubit) {
  54. if (n_qubit == 0) {
  55. int dim = 1UL << n_qubit;
  56. SparseMatrix out(dim, dim);
  57. out.reserve(VectorXi::Constant(dim, dim));
  58. for (int i = 0; i < dim; i++) {
  59. out.insert(i, i) = 1;
  60. }
  61. out.makeCompressed();
  62. return out;
  63. } else {
  64. SparseMatrix out = BasiGateSparse('I');
  65. for (int i = 1; i < n_qubit; i++) {
  66. out = KroneckerProductSparse(out, BasiGateSparse('I')).eval();
  67. }
  68. return out;
  69. }
  70. }
  71. SparseMatrix PauliTerm2Sparse(const PauliTerm &pt, Index _min, Index _max) {
  72. int poi;
  73. int n = pt.first.size();
  74. SparseMatrix out;
  75. if (pt.first[0].first == _min) {
  76. out = BasiGateSparse(pt.first[0].second) * pt.second;
  77. poi = 1;
  78. } else {
  79. out = BasiGateSparse('I') * pt.second;
  80. poi = 0;
  81. }
  82. for (Index i = _min + 1; i <= _max; i++) {
  83. if (poi == n) {
  84. out = KroneckerProductSparse(IdentitySparse(_max - i + 1), out).eval();
  85. break;
  86. } else {
  87. if (i == pt.first[poi].first) {
  88. out = KroneckerProductSparse(BasiGateSparse(pt.first[poi++].second), out).eval();
  89. } else {
  90. out = KroneckerProductSparse(BasiGateSparse('I'), out).eval();
  91. }
  92. }
  93. }
  94. return out;
  95. }
  96. SparseMatrix GoodTerm2Sparse(const GoodTerm &gt, Index n_qubits) {
  97. SparseMatrix out = PauliTerm2Sparse(gt.second[0], gt.first.second.first, gt.first.second.second);
  98. for (Index i = 1; i < gt.second.size(); i++) {
  99. out += PauliTerm2Sparse(gt.second[i], gt.first.second.first, gt.first.second.second);
  100. }
  101. out.prune({0.0, 0.0});
  102. out *= gt.first.first;
  103. out = KroneckerProductSparse(out, IdentitySparse(gt.first.second.first)).eval();
  104. out = KroneckerProductSparse(IdentitySparse(n_qubits - gt.first.second.second - 1), out).eval();
  105. return out;
  106. }
  107. } // namespace sparse
  108. } // namespace mindquantum
  109. } // namespace mindspore