{"id":4411,"date":"2018-03-05T20:43:44","date_gmt":"2018-03-05T12:43:44","guid":{"rendered":"http:\/\/www.systemsci.org\/jinshanw\/?p=4411"},"modified":"2018-03-06T10:51:51","modified_gmt":"2018-03-06T02:51:51","slug":"projectq%e5%92%8cibmq%e9%87%8f%e5%ad%90%e8%ae%a1%e7%ae%97%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/www.systemsci.org\/jinshanw\/2018\/03\/05\/projectq%e5%92%8cibmq%e9%87%8f%e5%ad%90%e8%ae%a1%e7%ae%97%e6%9c%ba\/","title":{"rendered":"ProjectQ\u548cIBMQ\u91cf\u5b50\u8ba1\u7b97\u673a"},"content":{"rendered":"<p><a href=\"http:\/\/research.ibm.com\/ibm-q\/\">IBMQ<\/a>\u63d0\u4f9b\u4e86\u4f7f\u7528\u91cf\u5b50\u8ba1\u7b97\u673a\u7684\u63a5\u53e3\uff0c<a href=\"https:\/\/projectq.ch\/\">ProjectQ<\/a>\u7528Python\u8bed\u8a00\u63d0\u4f9b\u4e86\u4e00\u5957\u91cf\u5b50\u8ba1\u7b97\u63cf\u8ff0\u8bed\u8a00\uff0c\u80fd\u591f\u7528\u4e8e\u5305\u542bIBMQ\u771f\u5b9e\u91cf\u5b50\u8ba1\u7b97\u673a\u4ee5\u53ca\u91cf\u5b50\u7684\u7ecf\u5178\u6a21\u62df\uff08\u901a\u8fc7\u7b97\u7b26\u8fd0\u7b97\uff09\u5728\u5185\u7684\u591a\u4e2a\u540e\u53f0\u3002<\/p>\n<p>\u7531\u4e8e\u6559\u5b66\uff08\u8ba9\u5b66\u751f\u505a\u4e60\u9898\u5e76\u4e14\u80fd\u591f\u5728\u5b9e\u9645\u91cf\u5b50\u8ba1\u7b97\u673a\u4e0a\u9a8c\u8bc1\uff09\u548c\u7814\u7a76\uff08\u8003\u8651\u91cf\u5b50\u535a\u5f08\u7684\u771f\u4eba\u5b9e\u9a8c\uff09\u7684\u9700\u8981\uff0c\u9700\u8981\u7528\u91cf\u5b50\u8ba1\u7b97\u673a\u3002\u5c31\u5b66\u4e60\u4e86\u4e00\u4e0b\u3002\u53d1\u73b0\uff0c\u975e\u5e38\u7b80\u5355\u6613\u7528\uff0c\u4e5f\u5f88\u6709\u7cfb\u7edf\u6027\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u91cf\u5b50\u8ba1\u7b97\u4e2d\uff0c\u6700\u57fa\u672c\u7684\u91cf\u5b50\u5355\u4f4d\u662f\u81ea\u65cb\u3002\u4e8e\u662f\uff0c\u81ea\u65cb\u7684\u72b6\u6001\uff08\u4e8c\u7ef4Hilbert\u7a7a\u95f4\u77e2\u91cf\uff09\u548c\u7b97\u7b26\uff08\u4e8c\u7ef4Hilbert\u7a7a\u95f4\u77e2\u91cf\u4e0a\u7684\u7b97\u7b26\uff0c\u4f8b\u5982Pauli\u77e9\u9635\u3001Hadamard\u95e8\u3001\u56f4\u7ed5Z\u8f74\u7684\u65cb\u8f6c\u3001\u6d4b\u91cf\u7b49\uff09\uff0c\u5c31\u662f\u8fd9\u4e2aProjectQ\u7684\u57fa\u672c\u5355\u5143\u3002\u5f53\u7136\uff0c\u4e00\u4e9b\u5e38\u7528\u7684\u9ad8\u7ea7\u529f\u80fd\u4f8b\u5982CNOT\u95e8\uff0c\u4ee5\u53caHadamard\u95e8\u548cCNOT\u5408\u8d77\u6765\u7684\u4f5c\u7528\u2014\u2014\u79f0\u4f5c\u7ea0\u7f20\u7b97\u7b26\u7b49\uff0c\u4e5f\u5728ProjectQ\u4e2d\u505a\u4e86\u5b9e\u73b0\u3002<\/p>\n<p>\u5177\u4f53\u7684\u6587\u6863\u89c1<a href=\"https:\/\/projectq.ch\/\">ProjectQ<\/a>\u548c<a href=\"http:\/\/research.ibm.com\/ibm-q\/\">IBMQ<\/a>\u4e0a\u7684\u8bf4\u660e\u3002\u4e0b\u9762\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u4e00\u4e0b\u6574\u4f53\u7ed3\u6784\uff0c\u8fd9\u6837\u4ee5\u540e\u518d\u4e00\u6b21\u5b66\u4e60\u8d77\u6765\u7b80\u5355\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u8bd5\u7740\u7f16\u5199\u7684\u7a0b\u5e8fquantum.py\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165\u540e\u53f0\uff08\u6a21\u62df\u5668\u3001IBMQ\u3001tex\uff09\u548c\u7b97\u7b26\uff08H\u3001\u6d4b\u91cf\u3001CNOT\uff09<\/li>\n<p><code><br \/>\nfrom projectq import MainEngine<br \/>\nfrom projectq.ops import H, Measure, CNOT<br \/>\nfrom projectq.backends import CircuitDrawer<br \/>\nimport projectq.setups.ibm<br \/>\nfrom projectq.backends import IBMBackend<br \/>\n<\/code><\/p>\n<li>\u8fd9\u90e8\u5206\u63a5\u6536\u7a0b\u5e8f\u8fd0\u884c\u53c2\u6570\uff0c\u5728\u8fd9\u91cc\u5c31\u662f\u51b3\u5b9a\u7528\u4ec0\u4e48\u540e\u53f0\u3002<\/li>\n<p><code><br \/>\nimport sys, getopt<\/p>\n<p>def main(argv):<br \/>\n\ts=1 #indicator of running this program via simulator or not<br \/>\n\ttex=0 #indicator of converting this program into tex or not<br \/>\n\tIBMQ=0 #indicator of running this program via IBMQ or not<br \/>\n\t#take parameters from command-line input<br \/>\n\ttry:<br \/>\n\t\topts, args = getopt.getopt(argv, \"s:\", [\"tex=\", \"IBMQ=\"])<br \/>\n\texcept getopt.GetoptError:<br \/>\n\t\tprint (\"Error: please use the command as quantum.py -s  --tex  --IBMQ \")<br \/>\n\t\tsys.exit(2)<br \/>\n\tfor opt, arg in opts:<br \/>\n\t\tif opt == \"-h\":<br \/>\n\t\t\tprint(\"quantum.py -r  --tex  --IBMQ \")<br \/>\n\t\t\tsys.exit()<br \/>\n\t\telif opt ==\"-s\":<br \/>\n\t\t\ts = int(arg)  #when s=1, we need to run this program via simulator<br \/>\n\t\telif opt == \"--IBMQ\":<br \/>\n\t\t\tIBMQ = int(arg) # when tex=1, we need to convert this program into a tex file<br \/>\n\t\t\ts = 0  #when tex=1, we set s=0 (not to run this program in force)<br \/>\n\t\telif opt == \"--tex\":<br \/>\n\t\t\ttex = int(arg) # when tex=1, we need to convert this program into a tex file<br \/>\n\t\t\ts = 0  #when tex=1, we set s=0 (not to run this program in force)<br \/>\n\t\t\tIBMQ = 0  #when tex=1, we set IBMQ=0 (not to run this program in force)<br \/>\n\t#parameter input ends here<br \/>\n<\/code><\/p>\n<li>\u6309\u7167\u8fd0\u884c\u53c2\u6570\uff0c\u5f00\u8f9f\u4e00\u4e2a\u9488\u5bf9\u76f8\u5e94\u540e\u53f0\u7684\u73af\u5883\u3002\u5728\u8fd9\u4e2a\u73af\u5883\u4e4b\u4e0a\uff0c\u6240\u6709\u7684\u547d\u4ee4\u4e0d\u518d\u9700\u8981\u5173\u5fc3\u540e\u53f0\u7684\u95ee\u9898\u3002<\/li>\n<p><code><br \/>\n\t# create a main compiler engine<br \/>\n\tif(tex):<br \/>\n\t\tdrawing_engine = CircuitDrawer()<br \/>\n\t\teng = MainEngine(drawing_engine)<br \/>\n\telif(s):<br \/>\n\t\teng = MainEngine()<br \/>\n\telif(IBMQ):<br \/>\n\t\teng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=False, device='ibmqx4'))<br \/>\n\t# allocate 2 qubit<br \/>\n<\/code><\/p>\n<li>\u5728\u73af\u5883\u91cc\u9762\u521d\u59cb\u5316qubit\uff0c\u5e76\u4e14\u628a\u9700\u8981\u7684\u7b97\u7b26\u4e00\u4e2a\u4e2a\u4f5c\u7528\u5230\u8fd9\u4e9b\u4e2aqubit\u4e0a\u3002<\/li>\n<p><code><br \/>\n\tqunum = eng.allocate_qureg(2)<\/p>\n<p>\t# put qubit 1 in superposition<br \/>\n\tH | qunum[0]<br \/>\n\t# put the two qubit in engtanglement<br \/>\n\tCNOT | (qunum[0],qunum[1])<br \/>\n\t# measure<br \/>\n\tMeasure | (qunum[0],qunum[1])<br \/>\n<\/code><\/p>\n<li>\u8fd0\u884c\u8fd9\u4e9b\u521d\u59cb\u5316\u72b6\u6001\u548c\u7b97\u7b26\u3002<\/li>\n<p><code><br \/>\n\teng.flush()<br \/>\n<\/code><\/p>\n<li>\u8f93\u51fa\u7ed3\u679c\u3002<\/li>\n<p><code><br \/>\n\tif(tex):<br \/>\n\t\tprint(drawing_engine.get_latex())<br \/>\n\tif(s):<br \/>\n\t\tprint(\"q1={}\".format(int(qunum[0])))<br \/>\n\t\tprint(\"q2={}\".format(int(qunum[1])))<br \/>\n\tif(IBMQ):<br \/>\n\t\t    # access the probabilities via the back-end:<br \/>\n\t\t\tresults = eng.backend.get_probabilities(qunum)<br \/>\n\t\t\tfor state in results:<br \/>\n\t\t\t\tprint(\"Measured {} with p = {}.\".format(state, results[state]))<br \/>\n\t\t\t# return one (random) measurement outcome.<br \/>\n\t\t\treturn [int(q) for q in qunum]<br \/>\n<\/code>\n<\/ol>\n<p>\u6700\u540e\u8fd0\u884c<code>python quantum.py<\/code>\u5c31\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u540e\u53f0\u8fd0\u884c\u4e86\u3002\u5982\u679c\u9700\u8981\u5728IBMQ\u4e0a\u8fd0\u884c\uff0c\u9700\u8981IBMQ\u7684\u5e10\u53f7\u548c\u5bc6\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IBMQ\u63d0\u4f9b\u4e86\u4f7f\u7528\u91cf\u5b50\u8ba1\u7b97\u673a\u7684\u63a5\u53e3\uff0cProjectQ\u7528Python\u8bed\u8a00\u63d0\u4f9b\u4e86\u4e00\u5957\u91cf\u5b50\u8ba1\u7b97\u63cf\u8ff0\u8bed\u8a00\uff0c\u80fd\u591f\u7528\u4e8e\u5305\u542b &hellip; <a href=\"https:\/\/www.systemsci.org\/jinshanw\/2018\/03\/05\/projectq%e5%92%8cibmq%e9%87%8f%e5%ad%90%e8%ae%a1%e7%ae%97%e6%9c%ba\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cProjectQ\u548cIBMQ\u91cf\u5b50\u8ba1\u7b97\u673a\u201d<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[369,370,371,368],"_links":{"self":[{"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/posts\/4411"}],"collection":[{"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/comments?post=4411"}],"version-history":[{"count":7,"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/posts\/4411\/revisions"}],"predecessor-version":[{"id":4418,"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/posts\/4411\/revisions\/4418"}],"wp:attachment":[{"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/media?parent=4411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/categories?post=4411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.systemsci.org\/jinshanw\/wp-json\/wp\/v2\/tags?post=4411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}