行翻转和列翻转_用量子计算机翻转硬币
行翻轉和列翻轉
Amazon recently announced its latest AWS offering: Quantum computing as a service. It’s called Amazon Braket (pronounced “bracket”). Last week it moved from a closed beta to general availability.
亞馬遜最近宣布了其最新的AWS產品:量子計算即服務。 它被稱為Amazon Braket (發音為“括號”)。 上周,它從一個封閉的Beta轉變為全面上市。
Here’s how you can run your first quantum program in just a few minutes.
這是您在短短幾分鐘內即可運行第一個量子程序的方式。
第1步:設置您的帳戶 (Step 1: Set Up Your Account)
The first thing you need to do is create an AWS account if you don’t have one already. Go to https://aws.amazon.com and follow the instructions.
您需要做的第一件事是創建一個AWS賬戶(如果您還沒有賬戶)。 轉到https://aws.amazon.com并按照說明進行操作。
Next, you’ll need to activate Amazon Braket by going to https://aws.amazon.com/braket/ and clicking “Get Started with Amazon Braket.”
接下來,您需要通過轉到https://aws.amazon.com/braket/并單擊“ Amazon Braket入門”來激活Amazon Braket。
This will walk you through some terms and conditions, which you’ll need to agree with to proceed. You’ll also be prompted to create an S3 bucket for storing the results of quantum programs. You’ll also have to set up access rules for the account. Fortunately, there are default settings filled in for you already. I just left every option alone. Make a note of the name of the S3 bucket for later.
這將引導您完成一些條款和條件,您需要同意這些條款和條件才能繼續進行。 系統還會提示您創建一個S3存儲桶,用于存儲量子程序的結果。 您還必須為該帳戶設置訪問規則。 幸運的是,已經為您填寫了默認設置。 我只留下了每個選項。 記下S3存儲桶的名稱,以備后用。
Next, click “notebooks” on the left and create a new notebook instance to run your code. Name it something meaningful and select the “ml.t3.medium” instance type. Keep in mind that this costs money to run. It is included in the AWS free tier, so you may be eligible to run up to 250 hours for free.
接下來,單擊左側的“筆記本”,然后創建一個新的筆記本實例來運行您的代碼。 將其命名為有意義的名稱,然后選擇“ ml.t3.medium”實例類型。 請記住,這需要花錢才能運行。 它包含在AWS免費套餐中,因此您可能有資格免費運行250小時。
There are some other settings for permissions and networking. Again, I just left the defaults.
權限和網絡還有其他一些設置。 同樣,我只是保留了默認值。
第2步:編寫一些代碼 (Step 2: Write Some Code)
Click the notebook URL shown on the notebook instance list. The URL will look something like: amazon-braket-[name].notebook.[region].sagemaker.aws
單擊筆記本實例列表上顯示的筆記本URL。 該URL類似于: amazon-braket-[name].notebook.[region].sagemaker.aws
This opens up a Jupyter Notebook environment. Click “New” on the top right then choose conda_braket. (This is the name of the Python virtual environment with all the needed dependencies pre-installed).
這將打開Jupyter Notebook環境。 單擊右上角的“新建”,然后選擇conda_braket 。 (這是預安裝所有必需依賴項的Python虛擬環境的名稱)。
Copy and run this code:
復制并運行以下代碼:
If all goes well, you will see a result like:
如果一切順利,您將看到類似以下結果:
Counter({'0': 505, '1': 495})We performed a simulation of running a single-qubit Hadamard gate and then measured the result 1,000 times. The counter shows the number of times the qubit was measured in the ground state ‘0’ and the excited state ‘1’.
我們對運行單量子位Hadamard門進行了仿真,然后對結果進行了1000次測量。 計數器顯示在基態“ 0”和激發態“ 1”下測量量子比特的次數。
The Hadamard gate puts the qubit in a superposition where it will have an equal probability of being measured in either state. This is exactly what we want: a coin flip with a 50/50 chance.
哈達瑪門將量子比特置于一個疊加中,在任一狀態下它都有相等的概率被測量。 這正是我們想要的:機會為50/50的硬幣翻轉。
But this example was just simulating a quantum computer. We want to use the real thing!
但是這個例子只是在模擬一臺量子計算機。 我們要使用真實的東西!
步驟3:選擇一臺Quantum計算機 (Step 3: Choose a Quantum Computer)
https://aws.amazon.com/braket/.https://aws.amazon.com/braket/ 。Amazon offers three different quantum computing devices to choose from:
亞馬遜提供三種不同的量子計算設備供您選擇:
The third option, quantum annealing from D-Wave, is a different paradigm of quantum computing that won’t work with the above code. So our choice is between using superconducting qubits or ion traps.
第三種選擇是D-Wave的量子退火,它是量子計算的另一范式,不適用于以上代碼。 因此,我們的選擇是在使用超導量子位或離子阱之間。
I’m going to choose the superconducting qubits from Rigetti since they have a cheaper per-shot rate than IonQ… and because I used to work at Rigetti.
我要從Rigetti選擇超導量子比特,因為它們的單次發射速率比IonQ便宜……并且因為我曾經在Rigetti工作。
In a new cell, copy the following. Here is where you’ll need the S3 folder from earlier because, unlike the free simulator, you don’t want to lose any results from real hardware. If you forgot the name of the S3 folder, you can look it up back in the AWS console under the S3 header.
在新的單元格中,復制以下內容。 在這里,您將需要較早版本的S3文件夾,因為與免費的模擬器不同,您不想丟失任何來自實際硬件的結果。 如果您忘記了S3文件夾的名稱,則可以在AWS控制臺中的S3標頭下查找它。
We’re reusing the coin_flip_circuit we created in the last step.
我們將重用在上一步中創建的coin_flip_circuit 。
If all goes well it should respond with:
如果一切順利,它應該以以下方式回應:
'CREATED'Now in a new cell (do not rerun the previous cell since that will enqueue another task!) keep running task.state() to see the status change to 'QUEUED'.
現在在新的單元格中(不要重新運行前一個單元格,因為這將使另一個任務入隊!)繼續運行task.state()以查看狀態更改為'QUEUED' 。
What’s happened? Your task hasn’t run on the quantum computer yet because it is waiting in line. Also, some of the quantum computers are only accessible during certain times of the day. It takes a lot of work to maintain properly calibrated quantum computing hardware, so it will only be available sparingly. Check the Devices tab on the Amazon Braket AWS console page to see the schedule for each device.
發生了什么? 您的任務尚未在量子計算機上運行,??因為它正在排隊。 同樣,某些量子計算機僅在一天中的特定時間可用。 維護正確校準的量子計算硬件需要花費大量工作,因此只能少量使用。 在Amazon Braket AWS控制臺頁面上查看設備選項卡,以查看每個設備的時間表。
In my case, I was scheduled to wait several hours for the next available slot. If you don’t want to keep my notebook server running for that amount of time, then you can run task.id and make a note of the returned value.
就我而言,我計劃等待幾個小時才能獲得下一個可用的廣告位。 如果您不想讓我的筆記本服務器在這段時間內運行,則可以運行task.id并記下返回值。
Later, you can run the following in a new cell (or even a new notebook) to recover the task:
以后,您可以在新的單元格(甚至新的筆記本)中運行以下命令以恢復任務:
Once task.state() returns 'COMPLETED' then you can access the results just like in the local simulator:
一旦task.state()返回'COMPLETED'您就可以像在本地模擬器中那樣訪問結果:
Again if all goes well, you’ll see a result like this:
同樣,如果一切順利,您將看到如下結果:
Counter({'0': 593, '1': 407})If you have the patience to repeat this a few times, you’ll likely notice that the ‘0’s seem to dominate the results. This is not a mistake, but more a reflection that today’s quantum computers aren’t perfectly accurate. In our example circuit, this results in more ‘0’s than ‘1’s.
如果您有耐心重復幾次,您可能會注意到“ 0”似乎主導了結果。 這不是一個錯誤,而是更多地反映出當今的量子計算機并不十分準確。 在我們的示例電路中,這導致比“ 1”更多的“ 0”。
By the way: To run on the ion traps instead replace device/qpu/rigetti/Aspen-8 with device/qpu/ionq/ionQdevice in the code above. It would be a good idea to reduce the number of shots since running 1000 shots costs 35 cents on Rigetti but 10 dollars on IonQ.
順便說一句:要在離子阱上運行, device/qpu/ionq/ionQdevice 在上面的代碼 device/qpu/rigetti/Aspen-8 替換為 device/qpu/ionq/ionQdevice 。 減少拍攝數量是一個好主意,因為在Rigetti上運行1000張照片要花費35美分,在IonQ上要花費10美元。
步驟4:放在一起 (Step 4: Put It All Together)
A complete coin flip example could look something like this:
一個完整的硬幣翻轉示例可能看起來像這樣:
Note: there is a minimum of 10 shots in Braket for some reason, but we only need one coin flip, so we ignore the other 9 shots.
注意:出于某些原因,Braket中至少有10張照片,但是我們只需要擲一次硬幣,因此我們忽略了另外9張照片。
This will (eventually) print either HEADS! or TAILS! depending on the measured state of the qubit.
(最終)這將打印兩個頭HEADS! 或TAILS! 取決于量子位的測量狀態。
Make sure to stop your notebook instance once you’re done!
完成后,請確保停止筆記本實例!
與經典的硬幣翻轉比較 (Comparing with a classical coin flip)
Here’s my review of the experience using a quantum computer to flip a coin vs. just flipping a physical coin.
這是我對使用量子計算機拋硬幣與僅拋硬幣的經驗的評論。
Flipping a coin with a quantum computer:
用量子計算機翻轉硬幣:
- 🚫 biased towards tails (although there are ways to work around this) ed偏于尾巴(盡管有解決此問題的方法)
- 🚫 costs money each flip flip每次翻轉都要花錢
- 🚫 only available during business hours 🚫僅在工作時間提供
- ? cool and quantum ?酷而量子
Flipping a physical coin:
翻轉實物硬幣:
- ? fair coin ?公平硬幣
- ? you need a coin, but you get to keep it afterward need你需要一枚硬幣,但是以后要保留它
- ? available any time any隨時可用
- 🚫 boring and classical classical無聊而古典
Overall, flipping a coin with a quantum computer is neat but not a very good way to decide who goes first in a football game. Nevertheless, quantum computers can still be a good source of randomness through the use of a randomness extractor. Some companies will go to great lengths to get a reliable source of truly random bits.
總的來說,用量子計算機擲硬幣是很整潔的,但不是決定誰在足球比賽中首屈一指的好方法。 盡管如此,通過使用隨機性提取器 ,量子計算機仍然可以成為良好的隨機性來源。 一些公司將竭盡全力以獲取可靠的真正隨機位的來源。
There are many other uses for a quantum computer, some of which you can find in the “Braket examples” folder on the notebook server. For instance, one notebook shows how to simulate molecule binding energies using the Variational Quantum Eigensolver (VQE) algorithm. There are other examples as well for basic quantum algorithms, quantum teleportation, and quantum optimization. As a next step, you can look through these and run the code there as well against real quantum devices.
量子計算機還有許多其他用途,其中一些可以在筆記本服務器的“ Braket示例”文件夾中找到。 例如,一個筆記本顯示了如何使用變分量子本征求解器(VQE)算法來模擬分子結合能。 基本量子算法,量子隱形傳態和量子優化也有其他示例。 下一步,您可以瀏覽這些代碼,并針對真正的量子設備在其中運行代碼。
Happy quantum computing!
快樂的量子計算!
翻譯自: https://medium.com/better-programming/flipping-a-coin-with-a-quantum-computer-4c8aec93fa27
行翻轉和列翻轉
總結
以上是生活随笔為你收集整理的行翻转和列翻转_用量子计算机翻转硬币的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 揭秘硅谷传奇:惠普的创业故事
- 下一篇: 掷硬币协议