[{"data":1,"prerenderedAt":597},["ShallowReactive",2],{"content-query-KvfzcUuySB":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"state":13,"body":14,"_type":591,"_id":592,"_source":593,"_file":594,"_stem":595,"_extension":596},"/activities/zh/3670","zh",false,"","第七届·2025 MindSpore量子计算黑客松全国大赛——热身赛通关攻略","无论您是初学小白，还是技术达人，参赛即有收获！报名截止日期5月20日，诚邀您报名参赛，勇攀量子世界的高峰，挑战黑客松年度总冠军，赢取属于你的荣誉和机遇！","2025-04-02","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/585ce2b394d54f85ba5cfbaf9e8720c6.png","activities",1,{"type":15,"children":16,"toc":579},"root",[17,25,30,43,51,69,88,93,98,103,114,121,126,131,138,165,172,180,185,190,195,200,205,213,218,231,236,244,249,254,259,264,272,283,288,297,302,311,331,338,345,350,355,362,367,378,383,388,395,400,405,410,417,428,443,450,457,464,469,480,485,492,497,502,509,514,523,531,540,545,557,568],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"第七届2025-mindspore量子计算黑客松全国大赛热身赛通关攻略",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":9},{"type":18,"tag":26,"props":31,"children":32},{},[33,35],{"type":24,"value":34},"大赛报名：",{"type":18,"tag":36,"props":37,"children":41},"a",{"href":38,"rel":39},"https://developer.huaweicloud.com/competition/information/1300000041",[40],"nofollow",[42],{"type":24,"value":38},{"type":18,"tag":26,"props":44,"children":45},{},[46],{"type":18,"tag":47,"props":48,"children":50},"img",{"alt":7,"src":49},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/6d504e1f579243089f350576370b5978.png",[],{"type":18,"tag":52,"props":53,"children":55},"h3",{"id":54},"_01-答题准备",[56,62,64],{"type":18,"tag":57,"props":58,"children":59},"strong",{},[60],{"type":24,"value":61},"# 01",{"type":24,"value":63}," ",{"type":18,"tag":57,"props":65,"children":66},{},[67],{"type":24,"value":68},"答题准备",{"type":18,"tag":26,"props":70,"children":71},{},[72,74,79,81,86],{"type":24,"value":73},"热身赛为量子计算基础学习和编程演练，完成热身赛的",{"type":18,"tag":57,"props":75,"children":76},{},[77],{"type":24,"value":78},"前100名",{"type":24,"value":80},"选手将有机会获得",{"type":18,"tag":57,"props":82,"children":83},{},[84],{"type":24,"value":85},"定制礼品和电子证书",{"type":24,"value":87},"，数量有限，不容错过。",{"type":18,"tag":26,"props":89,"children":90},{},[91],{"type":24,"value":92},"第一步：注册并完成华为云账号实名认证[1]；",{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":24,"value":97},"第二步：准备开发环境，详见本地安装使用MindSpore Quantum操作指导[2]；",{"type":18,"tag":26,"props":99,"children":100},{},[101],{"type":24,"value":102},"第三步：了解热身赛答题指南[3]；",{"type":18,"tag":26,"props":104,"children":105},{},[106,108],{"type":24,"value":107},"**热身赛题目文档：**",{"type":18,"tag":36,"props":109,"children":112},{"href":110,"rel":111},"https://developer.huaweicloud.com/competition/information/1300000041/html2",[40],[113],{"type":24,"value":110},{"type":18,"tag":26,"props":115,"children":116},{},[117],{"type":18,"tag":47,"props":118,"children":120},{"alt":7,"src":119},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/8c51ecffe371442292954f88f1a90b52.png",[],{"type":18,"tag":26,"props":122,"children":123},{},[124],{"type":24,"value":125},"每道题目的上方都有两个链接：MindSpore Quantum教程和MindSpore Quantum API参考，两个链接中都能找到题目的解答。答题小技巧：在MindSpore Quantum API参考中搜索题目中的关键词，可以快速找到对应的内容。",{"type":18,"tag":26,"props":127,"children":128},{},[129],{"type":24,"value":130},"**直播预告：**我们将于2025年4月8日 周二晚上19:00准时直播《第七届·2025 MindSpore量子计算黑客松全国大赛——热身赛通关攻略直播宣讲》",{"type":18,"tag":26,"props":132,"children":133},{},[134],{"type":18,"tag":47,"props":135,"children":137},{"alt":7,"src":136},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/644756eec96f4425ac871e83de7a3422.png",[],{"type":18,"tag":52,"props":139,"children":141},{"id":140},"_02-热身赛答题指导-一-题目1制备一个量子线路",[142,147,148,153,154,159,160],{"type":18,"tag":57,"props":143,"children":144},{},[145],{"type":24,"value":146},"# 02",{"type":24,"value":63},{"type":18,"tag":57,"props":149,"children":150},{},[151],{"type":24,"value":152},"热身赛答题指导",{"type":24,"value":63},{"type":18,"tag":57,"props":155,"children":156},{},[157],{"type":24,"value":158},"一",{"type":24,"value":63},{"type":18,"tag":57,"props":161,"children":162},{},[163],{"type":24,"value":164},"题目1：制备一个量子线路",{"type":18,"tag":26,"props":166,"children":167},{},[168],{"type":18,"tag":47,"props":169,"children":171},{"alt":7,"src":170},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/0cb889f9e13d4f2c8a6fea52e1ae04e9.png",[],{"type":18,"tag":26,"props":173,"children":174},{},[175],{"type":18,"tag":57,"props":176,"children":177},{},[178],{"type":24,"value":179},"题目描述：",{"type":18,"tag":26,"props":181,"children":182},{},[183],{"type":24,"value":184},"制备一个量子线路，该线路包含三个量子比特（q0, q1, q2）。线路由以下三个门组成：",{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":24,"value":189},"1、一个作用在q0比特上的H门。",{"type":18,"tag":26,"props":191,"children":192},{},[193],{"type":24,"value":194},"2、一个X门，作用在q1比特上，受q0比特控制。",{"type":18,"tag":26,"props":196,"children":197},{},[198],{"type":24,"value":199},"3、一个X门，作用在q2比特上，受q0和q1比特共同控制。",{"type":18,"tag":26,"props":201,"children":202},{},[203],{"type":24,"value":204},"题目图示：（这里对应 1.ipynb 中显示的量子线路图）",{"type":18,"tag":26,"props":206,"children":207},{},[208],{"type":18,"tag":57,"props":209,"children":210},{},[211],{"type":24,"value":212},"解题思路：",{"type":18,"tag":26,"props":214,"children":215},{},[216],{"type":24,"value":217},"这道题考查的是如何在MindSpore Quantum中构建基础的量子线路，特别是如何使用量子门的on方法来指定目标比特和控制比特。",{"type":18,"tag":219,"props":220,"children":221},"ul",{},[222],{"type":18,"tag":223,"props":224,"children":225},"li",{},[226],{"type":18,"tag":57,"props":227,"children":228},{},[229],{"type":24,"value":230},"on 方法：",{"type":18,"tag":26,"props":232,"children":233},{},[234],{"type":24,"value":235},"MindSpore Quantum中几乎所有的量子门都继承自BasicGate类，并拥有on()方法。这个方法的第一个参数是目标比特的索引（一个整数），第二个可选参数是控制比特的索引（可以是一个整数，或者一个包含多个整数的列表）。",{"type":18,"tag":26,"props":237,"children":238},{},[239],{"type":18,"tag":57,"props":240,"children":241},{},[242],{"type":24,"value":243},"解题步骤：",{"type":18,"tag":26,"props":245,"children":246},{},[247],{"type":24,"value":248},"1）对于作用在q0上的H门，目标比特是 0，没有控制比特，所以使用H.on(0)。",{"type":18,"tag":26,"props":250,"children":251},{},[252],{"type":24,"value":253},"2）对于作用在q1上、受q0控制的X门，目标比特是1，控制比特是0，所以使用 X.on(1, 0)。",{"type":18,"tag":26,"props":255,"children":256},{},[257],{"type":24,"value":258},"3）对于作用在q2上、受q0和q1控制的X门，目标比特是2，控制比特是0和1。当有多个控制比特时，需要将它们的索引放在一个列表里作为on方法的第二个参数，所以使用 X.on(2, [0, 1])。",{"type":18,"tag":26,"props":260,"children":261},{},[262],{"type":24,"value":263},"将这三步添加到Circuit对象中即可完成线路的构建。",{"type":18,"tag":26,"props":265,"children":266},{},[267],{"type":18,"tag":57,"props":268,"children":269},{},[270],{"type":24,"value":271},"相关API文档：",{"type":18,"tag":26,"props":273,"children":274},{},[275,277],{"type":24,"value":276},"Circuit 类: ",{"type":18,"tag":36,"props":278,"children":281},{"href":279,"rel":280},"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.Circuit.html",[40],[282],{"type":24,"value":279},{"type":18,"tag":26,"props":284,"children":285},{},[286],{"type":24,"value":287},"量子门模块(mindquantum.core.gates):",{"type":18,"tag":26,"props":289,"children":290},{},[291],{"type":18,"tag":36,"props":292,"children":295},{"href":293,"rel":294},"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/mindquantum.core.gates.html#module-mindquantum.core.gates",[40],[296],{"type":24,"value":293},{"type":18,"tag":26,"props":298,"children":299},{},[300],{"type":24,"value":301},"BasicGate.on 方法:",{"type":18,"tag":26,"props":303,"children":304},{},[305],{"type":18,"tag":36,"props":306,"children":309},{"href":307,"rel":308},"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.BasicGate.html#mindquantum.core.gates.BasicGate.on",[40],[310],{"type":24,"value":307},{"type":18,"tag":52,"props":312,"children":314},{"id":313},"二-题目2为量子线路添加控制比特",[315,325,326],{"type":18,"tag":57,"props":316,"children":317},{},[318,323],{"type":18,"tag":57,"props":319,"children":320},{},[321],{"type":24,"value":322},"二",{"type":24,"value":324},"、",{"type":24,"value":63},{"type":18,"tag":57,"props":327,"children":328},{},[329],{"type":24,"value":330},"题目2：为量子线路添加控制比特",{"type":18,"tag":26,"props":332,"children":333},{},[334],{"type":18,"tag":47,"props":335,"children":337},{"alt":7,"src":336},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/4465acb9a04344c991038eee542df1d8.png",[],{"type":18,"tag":26,"props":339,"children":340},{},[341],{"type":18,"tag":57,"props":342,"children":343},{},[344],{"type":24,"value":179},{"type":18,"tag":26,"props":346,"children":347},{},[348],{"type":24,"value":349},"给定一个已有的量子线路u1（在此例中是作用在q0和q1上的2比特量子傅里叶变换线路），要求使用q2作为控制比特，将其转换为一个受控量子线路u2。也就是说，u1中的每一个量子门操作，在u2中都将受到q2的控制。",{"type":18,"tag":26,"props":351,"children":352},{},[353],{"type":24,"value":354},"题目图示：（这里对应 2.ipynb 中显示的量子线路图）",{"type":18,"tag":26,"props":356,"children":357},{},[358],{"type":18,"tag":57,"props":359,"children":360},{},[361],{"type":24,"value":212},{"type":18,"tag":26,"props":363,"children":364},{},[365],{"type":24,"value":366},"这道题主要考查 mindquantum.core.circuit.controlled() 函数的使用。这个函数非常方便，它可以将一个完整的量子线路（或者单个算符）整体添加上控制位。",{"type":18,"tag":219,"props":368,"children":369},{},[370],{"type":18,"tag":223,"props":371,"children":372},{},[373],{"type":18,"tag":57,"props":374,"children":375},{},[376],{"type":24,"value":377},"controlled() 函数：",{"type":18,"tag":26,"props":379,"children":380},{},[381],{"type":24,"value":382},"它接受一个参数circuit_fn：需要被控制的对象，可以是一个Circuit 实例（如此题中的 u1），也可以是可以生成量子线路的函数。",{"type":18,"tag":26,"props":384,"children":385},{},[386],{"type":24,"value":387},"该函数返回一个新的函数，这个函数需要传入控制比特的索引，然后才会返回添加控制比特后的电路。",{"type":18,"tag":26,"props":389,"children":390},{},[391],{"type":18,"tag":57,"props":392,"children":393},{},[394],{"type":24,"value":243},{"type":18,"tag":26,"props":396,"children":397},{},[398],{"type":24,"value":399},"1）题目已经给出了原始线路u1=qft(range(2))。",{"type":18,"tag":26,"props":401,"children":402},{},[403],{"type":24,"value":404},"2）题目要求将q2添加为控制位。由于u1作用在q0和q1（索引为0和1），新添加的控制位q2的索引自然就是2。",{"type":18,"tag":26,"props":406,"children":407},{},[408],{"type":24,"value":409},"3）先调用 controlled(u1) 获取一个可以添加控制位的函数，再传入控制位索引2生成目标线路u2。该函数会自动处理u1中的所有门，为它们添加上索引为2的控制位。",{"type":18,"tag":26,"props":411,"children":412},{},[413],{"type":18,"tag":57,"props":414,"children":415},{},[416],{"type":24,"value":271},{"type":18,"tag":26,"props":418,"children":419},{},[420,422],{"type":24,"value":421},"controlled 数: ",{"type":18,"tag":36,"props":423,"children":426},{"href":424,"rel":425},"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.controlled.html",[40],[427],{"type":24,"value":424},{"type":18,"tag":52,"props":429,"children":431},{"id":430},"三-题目3量子线路的厄米共轭",[432,437,438],{"type":18,"tag":57,"props":433,"children":434},{},[435],{"type":24,"value":436},"三、",{"type":24,"value":63},{"type":18,"tag":57,"props":439,"children":440},{},[441],{"type":24,"value":442},"题目3：量子线路的厄米共轭",{"type":18,"tag":26,"props":444,"children":445},{},[446],{"type":18,"tag":47,"props":447,"children":449},{"alt":7,"src":448},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/f4eda93823a34375a6877b5c77a130f4.png",[],{"type":18,"tag":26,"props":451,"children":452},{},[453],{"type":18,"tag":47,"props":454,"children":456},{"alt":7,"src":455},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/04/03/f3b4282bb77e4670a672024108cfff13.png",[],{"type":18,"tag":26,"props":458,"children":459},{},[460],{"type":18,"tag":57,"props":461,"children":462},{},[463],{"type":24,"value":212},{"type":18,"tag":26,"props":465,"children":466},{},[467],{"type":24,"value":468},"这道题考查的是如何获取一个量子线路的厄米共轭形式。MindSpore Quantum提供了便捷的mindquantum.core.circuit.dagger()函数来完成这个任务。",{"type":18,"tag":219,"props":470,"children":471},{},[472],{"type":18,"tag":223,"props":473,"children":474},{},[475],{"type":18,"tag":57,"props":476,"children":477},{},[478],{"type":24,"value":479},"dagger() 函数：",{"type":18,"tag":26,"props":481,"children":482},{},[483],{"type":24,"value":484},"该函数接受一个 Circuit 对象（或其他支持厄米共轭操作的对象，如算符）作为输入，并返回一个新的 Circuit 对象，该对象即为输入线路的厄米共轭线路。它会自动处理门顺序的反转和每个门自身的厄米共轭替换。",{"type":18,"tag":26,"props":486,"children":487},{},[488],{"type":18,"tag":57,"props":489,"children":490},{},[491],{"type":24,"value":243},{"type":18,"tag":26,"props":493,"children":494},{},[495],{"type":24,"value":496},"1）首先，根据“原始线路”图示，构建出circ对象。这需要依次添加H门、受控T门、S门和受控X (CNOT)门。",{"type":18,"tag":26,"props":498,"children":499},{},[500],{"type":24,"value":501},"2）然后，直接调用 dagger(circ) 函数，将返回的厄米共轭线路赋值给dag_circ。",{"type":18,"tag":26,"props":503,"children":504},{},[505],{"type":18,"tag":57,"props":506,"children":507},{},[508],{"type":24,"value":271},{"type":18,"tag":26,"props":510,"children":511},{},[512],{"type":24,"value":513},"dagger 数:",{"type":18,"tag":26,"props":515,"children":516},{},[517],{"type":18,"tag":36,"props":518,"children":521},{"href":519,"rel":520},"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.dagger.html",[40],[522],{"type":24,"value":519},{"type":18,"tag":26,"props":524,"children":525},{},[526],{"type":18,"tag":57,"props":527,"children":528},{},[529],{"type":24,"value":530},"更多答题攻略，请点击下方链接查看：",{"type":18,"tag":26,"props":532,"children":533},{},[534],{"type":18,"tag":36,"props":535,"children":538},{"href":536,"rel":537},"https://h5.clewm.net/?url=qr61.cn%2FoxlATu%2Fqb38fRV&hasredirect=1",[40],[539],{"type":24,"value":536},{"type":18,"tag":26,"props":541,"children":542},{},[543],{"type":24,"value":544},"参考链接",{"type":18,"tag":26,"props":546,"children":547},{},[548,550],{"type":24,"value":549},"[1]",{"type":18,"tag":36,"props":551,"children":554},{"href":552,"rel":553},"https://support.huaweicloud.com/usermanual-account/zh-cn%5C_topic%5C_0077914254.html",[40],[555],{"type":24,"value":556},"https://support.huaweicloud.com/usermanual-account/zh-cn\\_topic\\_0077914254.html",{"type":18,"tag":26,"props":558,"children":559},{},[560,562],{"type":24,"value":561},"[2]",{"type":18,"tag":36,"props":563,"children":566},{"href":564,"rel":565},"https://www.hiascend.com/developer/blog/details/0272153713041042040",[40],[567],{"type":24,"value":564},{"type":18,"tag":26,"props":569,"children":570},{},[571,573],{"type":24,"value":572},"[3]",{"type":18,"tag":36,"props":574,"children":577},{"href":575,"rel":576},"https://www.hiascend.com/forum/thread-0201177851995961032-1-1.html",[40],[578],{"type":24,"value":575},{"title":7,"searchDepth":580,"depth":580,"links":581},4,[582,585,587,589],{"id":54,"depth":583,"text":584},3,"# 01 答题准备",{"id":140,"depth":583,"text":586},"# 02 热身赛答题指导 一 题目1：制备一个量子线路",{"id":313,"depth":583,"text":588},"二、 题目2：为量子线路添加控制比特",{"id":430,"depth":583,"text":590},"三、 题目3：量子线路的厄米共轭","markdown","content:activities:zh:3670.md","content","activities/zh/3670.md","activities/zh/3670","md",1776506029250]