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.

qaoa_mindquantum.py 2.6 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Benchmark for QAOA with MindQuantum"""
  16. import time
  17. import os
  18. from _parse_args import parser
  19. args = parser.parse_args()
  20. os.environ['OMP_NUM_THREADS'] = str(args.omp_num_threads)
  21. import numpy as np
  22. from mindquantum.ops import QubitOperator
  23. import mindspore.context as context
  24. import mindspore.dataset as ds
  25. import mindspore.nn as nn
  26. from mindspore import Model
  27. from mindspore.train.callback import LossMonitor
  28. from mindquantum import Hamiltonian
  29. from mindquantum import Circuit
  30. from mindquantum import RX, X, RZ, H
  31. from mindquantum.circuit import UN
  32. from mindquantum.nn import MindQuantumLayer
  33. def circuit_qaoa(p):
  34. circ = Circuit()
  35. circ += UN(H, n)
  36. for layer in range(p):
  37. for (u, v) in E:
  38. circ += X.on(v, u)
  39. circ += RZ('gamma_{}'.format(layer)).on(v)
  40. circ += X.on(v, u)
  41. for v in V:
  42. circ += RX('beta_{}'.format(layer)).on(v)
  43. return circ
  44. n = 12
  45. V = range(n)
  46. E = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0), (0, 3), (1, 4), (2, 6),
  47. (6, 7), (7, 8), (3, 8), (3, 9), (4, 9), (0, 10), (10, 11), (3, 11)]
  48. p = 4
  49. ITR = 120
  50. LR = 0.1
  51. ham = QubitOperator()
  52. for (v, u) in E:
  53. ham += QubitOperator('Z{} Z{}'.format(v, u), -1.0)
  54. ham = Hamiltonian(ham)
  55. circ = circuit_qaoa(p)
  56. ansatz_name = circ.parameter_resolver().para_name
  57. net = MindQuantumLayer(['null'], ansatz_name, RX('null').on(0) + circ, ham)
  58. train_loader = ds.NumpySlicesDataset({
  59. 'x': np.array([[0]]).astype(np.float32),
  60. 'y': np.array([0]).astype(np.float32)
  61. }).batch(1)
  62. class Loss(nn.MSELoss):
  63. """Loss"""
  64. def construct(self, base, target):
  65. return self.get_loss(-base)
  66. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  67. net_loss = Loss()
  68. net_opt = nn.Adam(net.trainable_params(), learning_rate=LR)
  69. model = Model(net, net_loss, net_opt)
  70. t0 = time.time()
  71. model.train(ITR, train_loader, callbacks=[LossMonitor()])
  72. t1 = time.time()
  73. print('Total time for mindquantum :{}'.format(t1 - t0))

MindQuantum是结合MindSpore和HiQ开发的量子机器学习框架,支持多种量子神经网络的训练和推理。得益于华为HiQ团队的量子计算模拟器和MindSpore高性能自动微分能力,MindQuantum能够高效处理量子机器学习、量子化学模拟和量子优化等问题,性能达到业界TOP1,为广大的科研人员、老师和学生提供了快速设计和验证量子机器学习算法的高效平台。