[{"data":1,"prerenderedAt":940},["ShallowReactive",2],{"content-query-Y6G1nm42VC":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"body":13,"_type":934,"_id":935,"_source":936,"_file":937,"_stem":938,"_extension":939},"/news/zh/2577","zh",false,"","参赛攻略 | 2023 MindSpore量子计算黑客松全国大赛 热身赛通关秘籍","假期赛程，炎炎夏日，潜研希尔伯特空间。初赛截止7月23日，决胜在8月17日。","2023-06-21","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2023/06/29/dbcf32f4f5234a7a8778478461e59e72.png","news",{"type":14,"children":15,"toc":931},"root",[16,24,34,43,68,78,86,94,104,128,135,143,154,159,171,182,194,199,210,217,235,247,252,261,269,279,297,302,311,319,333,338,345,353,363,382,387,396,401,410,418,428,446,451,461,469,479,491,498,510,515,524,529,539,547,554,562,570,578,590,600,607,612,619,624,632,639,651,658,663,671,676,681,690,698,708,727,735,740,745,754,759,768,776,786,796,821,832,840,845,850,860,865,874,882,893,904,911,921],{"type":17,"tag":18,"props":19,"children":21},"element","h1",{"id":20},"参赛攻略-2023-mindspore量子计算黑客松全国大赛-热身赛通关秘籍",[22],{"type":23,"value":8},"text",{"type":17,"tag":25,"props":26,"children":27},"p",{},[28],{"type":17,"tag":29,"props":30,"children":33},"img",{"alt":31,"src":32},"image.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072643.13471856494975790927709073218368:50540628033459:2400:995A0CD821AD3BFA2F92165E235D7FEFE5592AF6A9966523AC66272C83E0C461.png",[],{"type":17,"tag":25,"props":35,"children":36},{},[37],{"type":17,"tag":38,"props":39,"children":40},"strong",{},[41],{"type":23,"value":42},"今年大赛的四大亮点：",{"type":17,"tag":44,"props":45,"children":46},"ul",{},[47,53,58,63],{"type":17,"tag":48,"props":49,"children":50},"li",{},[51],{"type":23,"value":52},"黑客松大赛新增热身赛道，零基础起步，助您开启量子计算之旅，探索新世界。",{"type":17,"tag":48,"props":54,"children":55},{},[56],{"type":23,"value":57},"专家团队提供全方位服务：赛题解读和演练、课程培训、答疑等，帮助开发者快速成长。",{"type":17,"tag":48,"props":59,"children":60},{},[61],{"type":23,"value":62},"体验全新一代通用量子计算套件MindSpore Quantum和首发的量子模拟加速引擎QuPack。",{"type":17,"tag":48,"props":64,"children":65},{},[66],{"type":23,"value":67},"荣誉殿堂展播，黑客松决赛大奖、鼓励奖、推广奖等多重奖项可叠加。",{"type":17,"tag":25,"props":69,"children":70},{},[71,73],{"type":23,"value":72},"假期赛程，炎炎夏日，潜研希尔伯特空间。",{"type":17,"tag":38,"props":74,"children":75},{},[76],{"type":23,"value":77},"初赛截止7月23日，决胜在8月17日。",{"type":17,"tag":25,"props":79,"children":80},{},[81],{"type":17,"tag":38,"props":82,"children":83},{},[84],{"type":23,"value":85},"1",{"type":17,"tag":25,"props":87,"children":88},{},[89],{"type":17,"tag":38,"props":90,"children":91},{},[92],{"type":23,"value":93},"参赛对象和赛程安排",{"type":17,"tag":25,"props":95,"children":96},{},[97,102],{"type":17,"tag":38,"props":98,"children":99},{},[100],{"type":23,"value":101},"对量子计算感兴趣，具备基本Python编程能力的所有开发者",{"type":23,"value":103},"，均可报名参加。",{"type":17,"tag":25,"props":105,"children":106},{},[107,112,114,119,121,126],{"type":17,"tag":38,"props":108,"children":109},{},[110],{"type":23,"value":111},"热身赛",{"type":23,"value":113},"为量子计算基础学习和编程演练，初赛为量子应用算法开发，分",{"type":17,"tag":38,"props":115,"children":116},{},[117],{"type":23,"value":118},"量子化学模拟",{"type":23,"value":120},"和",{"type":17,"tag":38,"props":122,"children":123},{},[124],{"type":23,"value":125},"量子组合优化",{"type":23,"value":127},"两个独立赛道，可单选或同时挑战，决赛包括量子应用算法开发和答辩。",{"type":17,"tag":25,"props":129,"children":130},{},[131],{"type":17,"tag":29,"props":132,"children":134},{"alt":31,"src":133},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072708.77367399543294143540419059432468:50540628033459:2400:56F93D52E8305135B0AA453E038B731F9FBF2DCB78E1AEC76DFC066AD878B9AF.png",[],{"type":17,"tag":25,"props":136,"children":137},{},[138],{"type":17,"tag":38,"props":139,"children":140},{},[141],{"type":23,"value":142},"2",{"type":17,"tag":25,"props":144,"children":145},{},[146],{"type":17,"tag":38,"props":147,"children":148},{},[149],{"type":17,"tag":38,"props":150,"children":151},{},[152],{"type":23,"value":153},"热身赛答题指导",{"type":17,"tag":25,"props":155,"children":156},{},[157],{"type":23,"value":158},"热身赛为量子计算基础学习和编程演练。",{"type":17,"tag":25,"props":160,"children":161},{},[162,164,169],{"type":23,"value":163},"第一步：关注MindSpore Quantum代码仓，参见",{"type":17,"tag":38,"props":165,"children":166},{},[167],{"type":23,"value":168},"量子软件编程指南",{"type":23,"value":170},"。",{"type":17,"tag":25,"props":172,"children":173},{},[174,176,181],{"type":23,"value":175},"第二步：注册并完成",{"type":17,"tag":38,"props":177,"children":178},{},[179],{"type":23,"value":180},"华为云账号实名认证",{"type":23,"value":170},{"type":17,"tag":25,"props":183,"children":184},{},[185,187,192],{"type":23,"value":186},"第三步：登录",{"type":17,"tag":38,"props":188,"children":189},{},[190],{"type":23,"value":191},"HiQ量子计算云平台",{"type":23,"value":193},"进行在线答题。",{"type":17,"tag":25,"props":195,"children":196},{},[197],{"type":23,"value":198},"热身参考文档：",{"type":17,"tag":25,"props":200,"children":201},{},[202],{"type":17,"tag":203,"props":204,"children":208},"a",{"href":205,"rel":206},"https://competition.huaweicloud.com/informations/mobile/1000041884/circumstance",[207],"nofollow",[209],{"type":23,"value":205},{"type":17,"tag":25,"props":211,"children":212},{},[213],{"type":17,"tag":29,"props":214,"children":216},{"alt":31,"src":215},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072730.90035158382184656477518009861875:50540628033459:2400:1DEACD78733C954FB8FE5319DFCB8A382F2D24313C8CF3AB7287BF11142B341C.png",[],{"type":17,"tag":25,"props":218,"children":219},{},[220,222,226,228,233],{"type":23,"value":221},"可以看到，在每道题目的上方都有两个链接**：MindQuantum教程",{"type":17,"tag":38,"props":223,"children":224},{},[225],{"type":23,"value":120},{"type":23,"value":227},"MindQuantum API参考**。两个链接中都能找到题目的解答。小技巧：在",{"type":17,"tag":38,"props":229,"children":230},{},[231],{"type":23,"value":232},"MindQuantum API参考",{"type":23,"value":234},"中搜索题目中的关键词，可以快速找到对应的内容。",{"type":17,"tag":25,"props":236,"children":237},{},[238,240],{"type":23,"value":239},"MindQuantum教程：",{"type":17,"tag":203,"props":241,"children":244},{"href":242,"rel":243},"https://hiq.huaweicloud.com/tutorial/parameterized%5C_quantum%5C_circuit",[207],[245],{"type":23,"value":246},"https://hiq.huaweicloud.com/tutorial/parameterized\\_quantum\\_circuit",{"type":17,"tag":25,"props":248,"children":249},{},[250],{"type":23,"value":251},"MindQuantum API参考：",{"type":17,"tag":25,"props":253,"children":254},{},[255],{"type":17,"tag":203,"props":256,"children":259},{"href":257,"rel":258},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/index.html",[207],[260],{"type":23,"value":257},{"type":17,"tag":25,"props":262,"children":263},{},[264],{"type":17,"tag":38,"props":265,"children":266},{},[267],{"type":23,"value":268},"第一题：",{"type":17,"tag":25,"props":270,"children":271},{},[272],{"type":17,"tag":38,"props":273,"children":274},{},[275],{"type":17,"tag":29,"props":276,"children":278},{"alt":31,"src":277},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072751.65738660209797897332096464249524:50540628033459:2400:489FD2E29718B58C17E17EC6C443AE608C85D8E2E426EB48F408E053E09C44B0.png",[],{"type":17,"tag":25,"props":280,"children":281},{},[282,284,296],{"type":23,"value":283},"这道题考的是如何在线路中用on的方式添加量子门，在MindQuantum中，每个量子门都有on方法，比如X.on(0, 1)就意味着将X门作用在第0个比特上，并且受第一个比特控制。本题要求X门作用在第二个比特上，受第0个和第1个比特控制。因此可以用",{"type":17,"tag":38,"props":285,"children":286},{},[287,289,294],{"type":23,"value":288},"X.on(2, [0, 1])",{"type":17,"tag":38,"props":290,"children":291},{},[292],{"type":23,"value":293},"来表示。可以看到，on方法的第二个参数可以接受包含多个控制位的列表，因此可以方便的添加多控制门。详细用法可以参考",{"type":23,"value":295},"on方法的API文档",{"type":23,"value":170},{"type":17,"tag":25,"props":298,"children":299},{},[300],{"type":23,"value":301},"on方法的API文档：",{"type":17,"tag":25,"props":303,"children":304},{},[305],{"type":17,"tag":203,"props":306,"children":309},{"href":307,"rel":308},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.gates.html#mindquantum.core.gates.BasicGate.on",[207],[310],{"type":23,"value":307},{"type":17,"tag":25,"props":312,"children":313},{},[314],{"type":17,"tag":38,"props":315,"children":316},{},[317],{"type":23,"value":318},"点击下方链接，观看6月14日赛题讲解视频",{"type":17,"tag":25,"props":320,"children":321},{},[322,324],{"type":23,"value":323},"**蔻享：**",{"type":17,"tag":38,"props":325,"children":326},{},[327],{"type":17,"tag":203,"props":328,"children":331},{"href":329,"rel":330},"https://m.koushare.com/lives/room/261155",[207],[332],{"type":23,"value":329},{"type":17,"tag":25,"props":334,"children":335},{},[336],{"type":23,"value":337},"扫码观看赛题讲解视频",{"type":17,"tag":25,"props":339,"children":340},{},[341],{"type":17,"tag":29,"props":342,"children":344},{"alt":31,"src":343},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072822.90359211345042040110722703265162:50540628033459:2400:1383912CAE9A47270AE74C0A8ABCD2A5F43DDAADEAE808D8B0BF2DE3CA294F7B.png",[],{"type":17,"tag":25,"props":346,"children":347},{},[348],{"type":17,"tag":38,"props":349,"children":350},{},[351],{"type":23,"value":352},"第二题：",{"type":17,"tag":25,"props":354,"children":355},{},[356],{"type":17,"tag":38,"props":357,"children":358},{},[359],{"type":17,"tag":29,"props":360,"children":362},{"alt":31,"src":361},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072927.13733631412415746015524127491818:50540628033459:2400:D1F43B03E3F4DD8100475CC441FDC00C5CEA1DBBA9C6448258134E08F4324E38.png",[],{"type":17,"tag":25,"props":364,"children":365},{},[366,368,373,375,380],{"type":23,"value":367},"这道题考的是如何对量子线路进行压缩。在MindQuantum中，",{"type":17,"tag":38,"props":369,"children":370},{},[371],{"type":23,"value":372},"Circuit类",{"type":23,"value":374},"是量子线路模块，其中实现了很多操作量子线路的方法。其中就有一个方法可以压缩线路，接口名就叫作",{"type":17,"tag":38,"props":376,"children":377},{},[378],{"type":23,"value":379},"compress()",{"type":23,"value":381},"。因此，这道题调用**compress()**方法就可以成功通过了。",{"type":17,"tag":25,"props":383,"children":384},{},[385],{"type":23,"value":386},"Circuit类：",{"type":17,"tag":25,"props":388,"children":389},{},[390],{"type":17,"tag":203,"props":391,"children":394},{"href":392,"rel":393},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html#mindquantum.core.circuit.Circuit",[207],[395],{"type":23,"value":392},{"type":17,"tag":25,"props":397,"children":398},{},[399],{"type":23,"value":400},"compress()：",{"type":17,"tag":25,"props":402,"children":403},{},[404],{"type":17,"tag":203,"props":405,"children":408},{"href":406,"rel":407},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html#mindquantum.core.circuit.Circuit.compress",[207],[409],{"type":23,"value":406},{"type":17,"tag":25,"props":411,"children":412},{},[413],{"type":17,"tag":38,"props":414,"children":415},{},[416],{"type":23,"value":417},"第三题：",{"type":17,"tag":25,"props":419,"children":420},{},[421],{"type":17,"tag":38,"props":422,"children":423},{},[424],{"type":17,"tag":29,"props":425,"children":427},{"alt":31,"src":426},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628072945.29801863867614781001073081888890:50540628033459:2400:891CA35C78BCBDCA90AE8305D9807991F927473BD465726F98C61ACBF5BA44B0.png",[],{"type":17,"tag":25,"props":429,"children":430},{},[431,433,437,439,444],{"type":23,"value":432},"这道题考的是线路翻转，也就是说，我们需要将线路的比特顺序上下颠倒。题中给出了一个很长的线路，但是我们并不需要关心线路的具体内容，因为",{"type":17,"tag":38,"props":434,"children":435},{},[436],{"type":23,"value":372},{"type":23,"value":438},"中有一个方法可以直接实现线路翻转的功能——",{"type":17,"tag":38,"props":440,"children":441},{},[442],{"type":23,"value":443},"reverse_qubits()",{"type":23,"value":445},"，我们在代码中直接调用该方法就可以成功通过了。",{"type":17,"tag":25,"props":447,"children":448},{},[449],{"type":23,"value":450},"reverse_qubits()：",{"type":17,"tag":25,"props":452,"children":453},{},[454],{"type":17,"tag":203,"props":455,"children":458},{"href":456,"rel":457},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html#mindquantum.core.circuit.Circuit.reverse%5C_qubits",[207],[459],{"type":23,"value":460},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html#mindquantum.core.circuit.Circuit.reverse\\_qubits",{"type":17,"tag":25,"props":462,"children":463},{},[464],{"type":17,"tag":38,"props":465,"children":466},{},[467],{"type":23,"value":468},"第四题：",{"type":17,"tag":25,"props":470,"children":471},{},[472],{"type":17,"tag":38,"props":473,"children":474},{},[475],{"type":17,"tag":29,"props":476,"children":478},{"alt":31,"src":477},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073023.31813734753093084339596252115884:50540628033459:2400:63BA8B317E2542A4149F8FC4EF77EEC6E71C5BEEC31A690A8EE9CF4B2E0908D6.png",[],{"type":17,"tag":25,"props":480,"children":481},{},[482,484,489],{"type":23,"value":483},"这道题考的是如何给参数化量子线路中的变量名添加后缀，掌握这种技巧就可以很方便的搭建变分量子线路了。在",{"type":17,"tag":38,"props":485,"children":486},{},[487],{"type":23,"value":488},"mindquantum.core.circuit",{"type":23,"value":490},"模块中，**add_suffix()**函数就可以实现添加后缀的功能。具体来说，它需要接受一个量子线路和一个后缀字符串作为输入参数，然后返回一个添加了后缀的新的量子线路。选手只需要将这个新的量子线路通过+=的方式添加到原先的线路的后面，就可以构造出多层的变分线路了。题中的代码如下：",{"type":17,"tag":25,"props":492,"children":493},{},[494],{"type":17,"tag":29,"props":495,"children":497},{"alt":31,"src":496},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073041.64598406469413675323758487206615:50540628033459:2400:EFBEC1EBC2A245D146F0DD1C9B6B60FD425BC63CC2A14AD172794260C95C44A5.png",[],{"type":17,"tag":25,"props":499,"children":500},{},[501,503,508],{"type":23,"value":502},"首先我们需要将**add_suffix()",{"type":17,"tag":38,"props":504,"children":505},{},[506],{"type":23,"value":507},"函数 import 进来。题目中需要搭建3层线路，因此可以用一个循环实现，在每次循环中都对 new_circ 用+=的方式添加",{"type":23,"value":509},"add_suffix()**函数返回的新线路，后缀字符串设置为循环计数器的值p，就可以成功通过题目了。",{"type":17,"tag":25,"props":511,"children":512},{},[513],{"type":23,"value":514},"mindquantum.core.circuit：",{"type":17,"tag":25,"props":516,"children":517},{},[518],{"type":17,"tag":203,"props":519,"children":522},{"href":520,"rel":521},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html",[207],[523],{"type":23,"value":520},{"type":17,"tag":25,"props":525,"children":526},{},[527],{"type":23,"value":528},"add_suffix()：",{"type":17,"tag":25,"props":530,"children":531},{},[532],{"type":17,"tag":203,"props":533,"children":536},{"href":534,"rel":535},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html#mindquantum.core.circuit.add%5C_suffix",[207],[537],{"type":23,"value":538},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.circuit.html#mindquantum.core.circuit.add\\_suffix",{"type":17,"tag":25,"props":540,"children":541},{},[542],{"type":17,"tag":38,"props":543,"children":544},{},[545],{"type":23,"value":546},"第五题：",{"type":17,"tag":25,"props":548,"children":549},{},[550],{"type":17,"tag":29,"props":551,"children":553},{"alt":31,"src":552},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073057.43283872478775249377773633248979:50540628033459:2400:3DE7CF856D73E7935E5A2A8F4A2C2E82267054A12E8B4364A48E939CA1616A1A.png",[],{"type":17,"tag":25,"props":555,"children":556},{},[557],{"type":17,"tag":29,"props":558,"children":561},{"alt":559,"src":560},"cke_27374.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073121.19318487432746402081787937460418:50540628033459:2400:80176952AD2FBF3BC97E1751C13EBF73D477706A6F82EDD27CDB7F3E57C47739.png",[],{"type":17,"tag":25,"props":563,"children":564},{},[565],{"type":17,"tag":29,"props":566,"children":569},{"alt":567,"src":568},"cke_30883.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073149.36984650984457540902451252588665:50540628033459:2400:FEAF55F7BCCF75022EBF8766010BE40E7988F6E49B0937841C65E3B511D837B0.png",[],{"type":17,"tag":25,"props":571,"children":572},{},[573],{"type":17,"tag":38,"props":574,"children":575},{},[576],{"type":23,"value":577},"第六题：",{"type":17,"tag":25,"props":579,"children":580},{},[581,583,588],{"type":23,"value":582},"第6-9题都来源于同一个算法实践教程：",{"type":17,"tag":38,"props":584,"children":585},{},[586],{"type":23,"value":587},"通过量子神经网络对鸢尾花进行分类",{"type":23,"value":589},"，可以进入链接参考详细内容。",{"type":17,"tag":25,"props":591,"children":592},{},[593],{"type":17,"tag":203,"props":594,"children":597},{"href":595,"rel":596},"https://hiq.huaweicloud.com/tutorial/classification%5C_of%5C_iris%5C_by%5C_qnn",[207],[598],{"type":23,"value":599},"https://hiq.huaweicloud.com/tutorial/classification\\_of\\_iris\\_by\\_qnn",{"type":17,"tag":25,"props":601,"children":602},{},[603],{"type":17,"tag":29,"props":604,"children":606},{"alt":31,"src":605},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073218.84599809535769857981032371687836:50540628033459:2400:D16161CF6628E4BD36157E2EC85950173EC3486F5B267B85CC9DCA55DF0B8112.png",[],{"type":17,"tag":25,"props":608,"children":609},{},[610],{"type":23,"value":611},"此题需要我们将图中的encoder线路搭建出来。代码中已经搭建好了H和RZ这前两层，需要我们搭建的是红框部分。",{"type":17,"tag":25,"props":613,"children":614},{},[615],{"type":17,"tag":29,"props":616,"children":618},{"alt":31,"src":617},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073235.64359382643172888401227788690877:50540628033459:2400:AF26407185DBB72834B72A52A9E32889A6B755292C5B62D41157481588DEFF6D.png",[],{"type":17,"tag":25,"props":620,"children":621},{},[622],{"type":23,"value":623},"因此，我们要在12行的区域搭建CNOT+RZ+CNOT这样的线路结构。通过观察作用的比特位和循环计数器 j 的关系，可以这样实现：**X.on(j + 1, j)**是CNOT门，**RZ(f'alpha{j+4}').on(j + 1)**是含参RZ门，然后再作用一次CNOT门，这样就实现了encoder线路的搭建。",{"type":17,"tag":25,"props":625,"children":626},{},[627],{"type":17,"tag":38,"props":628,"children":629},{},[630],{"type":23,"value":631},"第七题：",{"type":17,"tag":25,"props":633,"children":634},{},[635],{"type":17,"tag":29,"props":636,"children":638},{"alt":31,"src":637},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073252.02905713285929095331509867109193:50540628033459:2400:33D11B6E7BB80484F41434CD9161B03DABE922F614A16B7C4092C4DDE2E47CDE.png",[],{"type":17,"tag":25,"props":640,"children":641},{},[642,644,649],{"type":23,"value":643},"此题需要我们将图中的ansatz线路搭建出来。代码中已经给出了",{"type":17,"tag":38,"props":645,"children":646},{},[647],{"type":23,"value":648},"HardwareEfficientAnsatz",{"type":23,"value":650},"模块，我们可以基于此模块搭建图中Ansatz。",{"type":17,"tag":25,"props":652,"children":653},{},[654],{"type":17,"tag":29,"props":655,"children":657},{"alt":31,"src":656},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073309.81372738798163827278518268379840:50540628033459:2400:07FC494A9899F6CDB88224CBF07D38B1D6C2B32B064CB8B835E2CB3A5EB371F0.png",[],{"type":17,"tag":25,"props":659,"children":660},{},[661],{"type":23,"value":662},"可以看到，Ansatz中需要RY旋转门，并且纠缠层用的CNOT门（受控X门），一共作用在4个比特上，需要3层线路。因此可以这样实现：",{"type":17,"tag":25,"props":664,"children":665},{},[666],{"type":17,"tag":38,"props":667,"children":668},{},[669],{"type":23,"value":670},"HardwareEfficientAnsatz(4, single_rot_gate_seq=[RY], entangle_gate=X, depth=3).circuit",{"type":17,"tag":25,"props":672,"children":673},{},[674],{"type":23,"value":675},"这样就完成了Ansatz线路的搭建。",{"type":17,"tag":25,"props":677,"children":678},{},[679],{"type":23,"value":680},"HardwareEfficientAnsatz：",{"type":17,"tag":25,"props":682,"children":683},{},[684],{"type":17,"tag":203,"props":685,"children":688},{"href":686,"rel":687},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.algorithm.nisq.html#mindquantum.algorithm.nisq.HardwareEfficientAnsatz",[207],[689],{"type":23,"value":686},{"type":17,"tag":25,"props":691,"children":692},{},[693],{"type":17,"tag":38,"props":694,"children":695},{},[696],{"type":23,"value":697},"第八题：",{"type":17,"tag":25,"props":699,"children":700},{},[701],{"type":17,"tag":38,"props":702,"children":703},{},[704],{"type":17,"tag":29,"props":705,"children":707},{"alt":31,"src":706},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073334.52122951538053736757137556173550:50540628033459:2400:7551923595D595D82282064DE9D30AE99018989C873462051EFBA96D35BE457B.png",[],{"type":17,"tag":25,"props":709,"children":710},{},[711,713,718,720,725],{"type":23,"value":712},"此题需要我们构造出哈密顿量Z2，Z3。在MindQuantum中，",{"type":17,"tag":38,"props":714,"children":715},{},[716],{"type":23,"value":717},"Hamiltonian",{"type":23,"value":719},"模块是一个包装器，可以将用",{"type":17,"tag":38,"props":721,"children":722},{},[723],{"type":23,"value":724},"Qubitoperator",{"type":23,"value":726},"构造的一些泡利算符包装成哈密顿量。因此这题可以这样实现：",{"type":17,"tag":25,"props":728,"children":729},{},[730],{"type":17,"tag":38,"props":731,"children":732},{},[733],{"type":23,"value":734},"[Hamiltonian(QubitOperator(f'Z{i}')) for i in [2, 3]]",{"type":17,"tag":25,"props":736,"children":737},{},[738],{"type":23,"value":739},"这样就完成了哈密顿量的构造。",{"type":17,"tag":25,"props":741,"children":742},{},[743],{"type":23,"value":744},"Hamiltonian：",{"type":17,"tag":25,"props":746,"children":747},{},[748],{"type":17,"tag":203,"props":749,"children":752},{"href":750,"rel":751},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.operators.html#mindquantum.core.operators.Hamiltonian",[207],[753],{"type":23,"value":750},{"type":17,"tag":25,"props":755,"children":756},{},[757],{"type":23,"value":758},"Qubitoperator：",{"type":17,"tag":25,"props":760,"children":761},{},[762],{"type":17,"tag":203,"props":763,"children":766},{"href":764,"rel":765},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.core.operators.html#mindquantum.core.operators.QubitOperator",[207],[767],{"type":23,"value":764},{"type":17,"tag":25,"props":769,"children":770},{},[771],{"type":17,"tag":38,"props":772,"children":773},{},[774],{"type":23,"value":775},"第九题：",{"type":17,"tag":25,"props":777,"children":778},{},[779],{"type":17,"tag":38,"props":780,"children":781},{},[782],{"type":17,"tag":29,"props":783,"children":785},{"alt":31,"src":784},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073353.50004925809712397144124959034381:50540628033459:2400:4F9E2A491B7EED7E8B547052A5FA10D928B1CB2C7D1563EF908295F849F7A24A.png",[],{"type":17,"tag":25,"props":787,"children":788},{},[789],{"type":17,"tag":38,"props":790,"children":791},{},[792],{"type":17,"tag":29,"props":793,"children":795},{"alt":31,"src":794},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073402.94452072289841048638194044308585:50540628033459:2400:623988AA43AADDC5546B9899921FD0743102F7990F514089552A142FBC59CFA3.png",[],{"type":17,"tag":25,"props":797,"children":798},{},[799,801,806,808,812,814,819],{"type":23,"value":800},"此题需要我们用",{"type":17,"tag":38,"props":802,"children":803},{},[804],{"type":23,"value":805},"get_expectation_with_grad",{"type":23,"value":807},"接口得到先前搭建的变分量子线路的梯度算子。",{"type":17,"tag":38,"props":809,"children":810},{},[811],{"type":23,"value":805},{"type":23,"value":813},"接口是",{"type":17,"tag":38,"props":815,"children":816},{},[817],{"type":23,"value":818},"mindquantum.simulator.Simulator",{"type":23,"value":820},"类的一个方法，接受哈密顿量和线路，返回一个梯度算子。这个梯度算子可以接受变分线路中参数的值，然后返回对应的期望值和梯度。",{"type":17,"tag":25,"props":822,"children":823},{},[824,826,830],{"type":23,"value":825},"因此，我们可以将先前构造的哈密顿量和线路输入到",{"type":17,"tag":38,"props":827,"children":828},{},[829],{"type":23,"value":805},{"type":23,"value":831},"接口中：",{"type":17,"tag":25,"props":833,"children":834},{},[835],{"type":17,"tag":38,"props":836,"children":837},{},[838],{"type":23,"value":839},"grad_ops = sim.get_expectation_with_grad(hams, circuit)",{"type":17,"tag":25,"props":841,"children":842},{},[843],{"type":23,"value":844},"这样就生成了可以计算期望值和梯度的算子grad_ops。",{"type":17,"tag":25,"props":846,"children":847},{},[848],{"type":23,"value":849},"get_expectation_with_grad：",{"type":17,"tag":25,"props":851,"children":852},{},[853],{"type":17,"tag":203,"props":854,"children":857},{"href":855,"rel":856},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.simulator.html#mindquantum.simulator.Simulator.get%5C_expectation%5C_with%5C_grad",[207],[858],{"type":23,"value":859},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.simulator.html#mindquantum.simulator.Simulator.get\\_expectation\\_with\\_grad",{"type":17,"tag":25,"props":861,"children":862},{},[863],{"type":23,"value":864},"mindquantum.simulator.Simulator：",{"type":17,"tag":25,"props":866,"children":867},{},[868],{"type":17,"tag":203,"props":869,"children":872},{"href":870,"rel":871},"https://mindspore.cn/mindquantum/docs/zh-CN/r0.8/mindquantum.simulator.html#mindquantum.simulator.Simulator",[207],[873],{"type":23,"value":870},{"type":17,"tag":25,"props":875,"children":876},{},[877],{"type":17,"tag":38,"props":878,"children":879},{},[880],{"type":23,"value":881},"3",{"type":17,"tag":25,"props":883,"children":884},{},[885],{"type":17,"tag":38,"props":886,"children":887},{},[888],{"type":17,"tag":38,"props":889,"children":890},{},[891],{"type":23,"value":892},"大赛报名&邀请好友参赛",{"type":17,"tag":25,"props":894,"children":895},{},[896,898],{"type":23,"value":897},"点击查看奖品和操作步骤：",{"type":17,"tag":203,"props":899,"children":902},{"href":900,"rel":901},"https://competition.huaweicloud.com/information/1000041884/invite",[207],[903],{"type":23,"value":900},{"type":17,"tag":25,"props":905,"children":906},{},[907],{"type":17,"tag":29,"props":908,"children":910},{"alt":31,"src":909},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/e64/154/b38/90a1d5d431e64154b387b3660e356ff5.20230628073423.40182835892316712336103643867550:50540628033459:2400:D19076A0DAC9506E010789750D7B274D1E81D302A5D193AED71B9A5662C55CC4.png",[],{"type":17,"tag":25,"props":912,"children":913},{},[914],{"type":17,"tag":203,"props":915,"children":918},{"href":916,"rel":917},"http://mp.weixin.qq.com/s?__biz=MzI3NjAzMjA0NA==&mid=2247504596&idx=1&sn=43e7407199639455b50eae181ade4573&chksm=eb792d97dc0ea481b2c7dfde00ca452dfde8d6a551754dc7177eb056edfafbbc36acf8db22b6&scene=21#wechat_redirect",[207],[919],{"type":23,"value":920},"大赛报名 | 2023 MindSpore量子计算黑客松全国大赛正式开启，新增热身赛道，小白和达人一起来挑战",{"type":17,"tag":25,"props":922,"children":923},{},[924],{"type":17,"tag":203,"props":925,"children":928},{"href":926,"rel":927},"http://mp.weixin.qq.com/s?__biz=MzI3NjAzMjA0NA==&mid=2247505224&idx=1&sn=f20c8b1c8aa439c7421e831e226fa7f5&chksm=eb792a0bdc0ea31d46984d6e7f99992aa33894ec2987f262f34a116bfabe9b04fb840b2d72f7&scene=21#wechat_redirect",[207],[929],{"type":23,"value":930},"邀请好友赢好礼 | 2023MindSpore量子计算黑客松全国大赛邀请好友参赛通道正式开启，众多好礼等你来拿！",{"title":7,"searchDepth":932,"depth":932,"links":933},4,[],"markdown","content:news:zh:2577.md","content","news/zh/2577.md","news/zh/2577","md",1776506067689]