# Get started with Fire Opal on IonQ through Amazon Braket

> Run your first quantum circuit on a real IonQ quantum device

Source: https://docs.q-ctrl.com/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket

You're ready to use a quantum computer and you'd like to use Fire Opal to submit a program.

In this tutorial, you'll run a quantum program consisting of a single Bernstein–Vazirani circuit using the Fire Opal Python package. This guide provides the quantum circuit for you. All you will need to do is set up your Amazon Braket account and bring your AWS and Q-CTRL credentials.

## Requirements
- An [Amazon Braket account](https://aws.amazon.com/braket/)
- A Q-CTRL account for [Fire Opal](https://q-ctrl.com/fire-opal)
- The latest version of the [Fire Opal Python package](https://docs.q-ctrl.com/fire-opal/discover/start-using/how-to-set-up-and-install-fire-opal.md)
- Familiarity with running Jupyter notebooks and Python environments
- Internet access

## Step 1: Import required libraries


```python
import fireopal as fo
import qiskit
import matplotlib.pyplot as plt
from braket.circuits.serialization import IRType
from braket.circuits import Circuit
from braket.aws import AwsDevice
import qctrlvisualizer as qv
```

>**Note:** If you experience an error while attempting to import the above libraries, please run the following command using the terminal. Please note that after installing new packages, you will need to restart your notebook session or kernel for the newly installed packages to be recognized.


```python
%pip install fire-opal matplotlib qiskit qctrl-visualizer amazon-braket-sdk
```

## Step 2: Declare the quantum program that will be run

Below we have pre-defined a 31-qubit Bernstein–Vazirani circuit in the [OpenQASM](https://openqasm.com/) string format. The OpenQASM format may be generated by exporting a quantum circuit written with any quantum-specific Python library, such as qiskit.


```python
shot_count = 2048
hidden_string = "111111111111111111111111111111"

circuit_qasm = """
OPENQASM 3.0;
include "stdgates.inc";
qubit[31] q;
bit[30] meas;
x q[30];
h q[0];
h q[1];
h q[2];
h q[3];
h q[4];
h q[5];
h q[6];
h q[7];
h q[8];
h q[9];
h q[10];
h q[11];
h q[12];
h q[13];
h q[14];
h q[15];
h q[16];
h q[17];
h q[18];
h q[19];
h q[20];
h q[21];
h q[22];
h q[23];
h q[24];
h q[25];
h q[26];
h q[27];
h q[28];
h q[29];
h q[30];
cx q[0], q[30];
cx q[1], q[30];
cx q[2], q[30];
cx q[3], q[30];
cx q[4], q[30];
cx q[5], q[30];
cx q[6], q[30];
cx q[7], q[30];
cx q[8], q[30];
cx q[9], q[30];
cx q[10], q[30];
cx q[11], q[30];
cx q[12], q[30];
cx q[13], q[30];
cx q[14], q[30];
cx q[15], q[30];
cx q[16], q[30];
cx q[17], q[30];
cx q[18], q[30];
cx q[19], q[30];
cx q[20], q[30];
cx q[21], q[30];
cx q[22], q[30];
cx q[23], q[30];
cx q[24], q[30];
cx q[25], q[30];
cx q[26], q[30];
cx q[27], q[30];
cx q[28], q[30];
cx q[29], q[30];
barrier q[0], q[1], q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9], q[10], q[11], q[12], q[13], q[14], q[15], q[16], q[17], q[18], q[19], q[20], q[21], q[22], q[23], q[24], q[25], q[26], q[27], q[28], q[29], q[30];
h q[0];
h q[1];
h q[2];
h q[3];
h q[4];
h q[5];
h q[6];
h q[7];
h q[8];
h q[9];
h q[10];
h q[11];
h q[12];
h q[13];
h q[14];
h q[15];
h q[16];
h q[17];
h q[18];
h q[19];
h q[20];
h q[21];
h q[22];
h q[23];
h q[24];
h q[25];
h q[26];
h q[27];
h q[28];
h q[29];
h q[30];
barrier q[0], q[1], q[2], q[3], q[4], q[5], q[6], q[7], q[8], q[9], q[10], q[11], q[12], q[13], q[14], q[15], q[16], q[17], q[18], q[19], q[20], q[21], q[22], q[23], q[24], q[25], q[26], q[27], q[28], q[29], q[30];
meas[0] = measure q[0];
meas[1] = measure q[1];
meas[2] = measure q[2];
meas[3] = measure q[3];
meas[4] = measure q[4];
meas[5] = measure q[5];
meas[6] = measure q[6];
meas[7] = measure q[7];
meas[8] = measure q[8];
meas[9] = measure q[9];
meas[10] = measure q[10];
meas[11] = measure q[11];
meas[12] = measure q[12];
meas[13] = measure q[13];
meas[14] = measure q[14];
meas[15] = measure q[15];
meas[16] = measure q[16];
meas[17] = measure q[17];
meas[18] = measure q[18];
meas[19] = measure q[19];
meas[20] = measure q[20];
meas[21] = measure q[21];
meas[22] = measure q[22];
meas[23] = measure q[23];
meas[24] = measure q[24];
meas[25] = measure q[25];
meas[26] = measure q[26];
meas[27] = measure q[27];
meas[28] = measure q[28];
meas[29] = measure q[29];
"""
```

This quantum circuit was selected for this tutorial because the results of running it can be understood in simple terms. If the quantum computer was entirely error-free, the `hidden_string` defined above would be read out 100% of the time. Later in this tutorial, we will visualize how often the hidden all `1`s bitstring is measured as a percentage of the number of total measurements, or `shot_count`.

### Step 2.1: Visualize the quantum circuit

For your convenience, you may visualize the quantum circuit using the `draw_circuit` function provided below.


```python
def draw_circuit(qasm_str: str):
    qasm_str = qasm_str.replace("OPENQASM 3.0;", "OPENQASM 2.0;")
    qasm_str = qasm_str.replace("stdgates.inc", "qelib1.inc")
    qasm_str = qasm_str.replace("bit[30] meas", "creg c[30]").replace(
        "qubit[31] q", "qreg q[31]"
    )
    qasm_str = (
        qasm_str.replace("meas[", "measure q[")
        .replace("] = measure q[", "] -> c[")
        .replace("];", "];")
    )
    circuit = qiskit.QuantumCircuit.from_qasm_str(qasm_str)
    display(circuit.draw(fold=-1))


draw_circuit(circuit_qasm)
```


<pre style="word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: &quot;Courier New&quot;,Courier,monospace">      ┌───┐                                                                                                                                                            ░ ┌───┐ ░ ┌─┐                                                                                       
 q_0: ┤ H ├───────■────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░─┤M├───────────────────────────────────────────────────────────────────────────────────────
      ├───┤       │                                                                                                                                                    ░ ├───┤ ░ └╥┘┌─┐                                                                                    
 q_1: ┤ H ├───────┼────■───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫─┤M├────────────────────────────────────────────────────────────────────────────────────
      ├───┤       │    │                                                                                                                                               ░ ├───┤ ░  ║ └╥┘┌─┐                                                                                 
 q_2: ┤ H ├───────┼────┼────■──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫─┤M├─────────────────────────────────────────────────────────────────────────────────
      ├───┤       │    │    │                                                                                                                                          ░ ├───┤ ░  ║  ║ └╥┘┌─┐                                                                              
 q_3: ┤ H ├───────┼────┼────┼────■─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫─┤M├──────────────────────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │                                                                                                                                     ░ ├───┤ ░  ║  ║  ║ └╥┘┌─┐                                                                           
 q_4: ┤ H ├───────┼────┼────┼────┼────■────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫─┤M├───────────────────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │                                                                                                                                ░ ├───┤ ░  ║  ║  ║  ║ └╥┘┌─┐                                                                        
 q_5: ┤ H ├───────┼────┼────┼────┼────┼────■───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫─┤M├────────────────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │                                                                                                                           ░ ├───┤ ░  ║  ║  ║  ║  ║ └╥┘┌─┐                                                                     
 q_6: ┤ H ├───────┼────┼────┼────┼────┼────┼────■──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫─┤M├─────────────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │                                                                                                                      ░ ├───┤ ░  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                                  
 q_7: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────■─────────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │                                                                                                                 ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                               
 q_8: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────■────────────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │                                                                                                            ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                            
 q_9: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────■───────────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │                                                                                                       ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                         
q_10: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■──────────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │                                                                                                  ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                      
q_11: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■─────────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │                                                                                             ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                   
q_12: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■────────────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │                                                                                        ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                                
q_13: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■───────────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                                                   ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                             
q_14: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■──────────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                                              ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                          
q_15: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■─────────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                                         ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                       
q_16: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■────────────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                                    ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                    
q_17: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■───────────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                               ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                                 
q_18: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■──────────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                          ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                              
q_19: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■─────────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                     ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                           
q_20: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■────────────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                                ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                        
q_21: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■───────────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                           ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                     
q_22: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■──────────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                      ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐                  
q_23: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■─────────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                                 ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐               
q_24: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■────────────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                            ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐            
q_25: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■───────────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                       ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐         
q_26: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■──────────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │                  ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐      
q_27: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■─────────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │             ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐   
q_28: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■────────░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
      ├───┤       │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │        ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
q_29: ┤ H ├───────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────■───░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
      ├───┤┌───┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐┌─┴─┐ ░ ├───┤ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
q_30: ┤ X ├┤ H ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├┤ X ├─░─┤ H ├─░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─
      └───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘ ░ └───┘ ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ 
c: 30/════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                                                                                                                                                                                  0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29</pre>


As you can see here, the circuit utilizes qubits numbered 0 to 30, and due to the nature of the Bernstein–Vazirani program, 30 qubits are measured, resulting in bitstring outputs of length 30.

## Step 3: Input your Amazon Braket credentials

This step sets up a `credentials` object which allows Fire Opal to utilize your Amazon Braket account on your behalf for running quantum programs as well as checking information such as which supported devices you have access to. To set this up, you'll have to find your Amazon Braket credentials on their website and input them in the cell that follows.

### Step 3.1: Set up Amazon Braket and create a cross-account IAM role

Fire Opal supports IonQ backends on Amazon Braket. You will need to set up an AWS account with access to Braket following the steps in this [set up guide](https://aws.amazon.com/blogs/quantum-computing/setting-up-your-local-development-environment-in-amazon-braket/) if you are an AWS administrator, contact your AWS administrator if you have a managed account. After setting up your AWS account to access Braket. Here is how to create a cross-account IAM role to allow Fire Opal to use Braket:

1. Navigate and sign into [IAM create role page](https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/roles/create)
2. Select **AWS Account** for **Trusted entity type**


![IAM_AWS.png-1](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/iam-aws-png-1.webp)


3. Select **Another AWS account** and type **522814692014** 


![account_id.png-2](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/account-id-png-2.webp)


4. Select **Require external ID** and type in your **Q-CTRL organization ID**, accessible at [Q-CTRL Accounts](https://accounts.q-ctrl.com/)


![external_id.png-3](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/external-id-png-3.webp)


5. Click **Next** and search and check the **AmazonBraketFullAccess** policy for **Add permissions**


![full_access.png-4](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/full-access-png-4.webp)


6. Click **Next** and enter a role name for **Role details** and keep note of it as you will use it later.


![role_name.png-5](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/role-name-png-5.webp)


7. Click **Create role**

### Step 3.2: Define your Amazon Braket credentials in code

Next, you will want to create `credentials` which will enable you to submit quantum tasks to IonQ devices using the Fire Opal Python package.

#### Step 3.2.1: Authenticate your Q-CTRL account using an API key

Because Fire Opal requires an account, you must first authenticate with Fire Opal. When using an AWS web based environment, you’ll have to authenticate with an API key. You may follow the instructions to [authenticate using an API key](https://docs.q-ctrl.com/fire-opal/discover/start-using/how-to-set-up-and-install-fire-opal.md), or briefly, you can do so by navigating to your [Q-CTRL account](https://accounts.q-ctrl.com/security?organization=), locating your API key, pasting it below and running the following cell.


```python
fo.authenticate_qctrl_account(api_key="Your Q-CTRL API Key")
```

    Q-CTRL authentication successful!


### Step 3.2.2: (Optional) Specifying your Q-CTRL organization

If your Q-CTRL account is a member of multiple organizations, you must specify which organization to use by setting the organization parameter, replace `organization_slug` with the unique identifier listed in your organization details. You can check organization names and other details by visiting [Q-CTRL Accounts](https://accounts.q-ctrl.com/).  


```python
fo.config.configure_organization(organization_slug="organization_slug")
```

Now that you have authenticated your Q-CTRL account, run the following cell to create `credentials`. 


```python
credentials = fo.credentials.make_credentials_for_braket(arn="your_role_arn")
```

## Step 4: Select the IonQ quantum device

There are several ways to determine your IonQ device access. Fire Opal provides a programmatic way to obtain this information with the function `show_supported_devices`. We highly recommend using this function as it cross-compares your accessible devices with those supported by Fire Opal. 


```python
supported_devices = fo.show_supported_devices(credentials=credentials)[
    "supported_devices"
]
for name in supported_devices:
    print(name)
```

## Step 5: Submit the quantum program to run

When you are ready to select one of the devices above, copy and paste the device's name below to set the `backend_name`. Then, run the following cell to submit the circuit defined in Step 2 to run on that device.


```python
backend_name = "PASTE_NAME_FROM_PREVIOUS_STEP"  # Run Step 4 to obtain device names

fire_opal_job = fo.execute(
    circuits=[circuit_qasm],
    shot_count=shot_count,
    credentials=credentials,
    backend_name=backend_name,
)
```

### Step 5.1: Retrieve the results

Running a quantum program may take anywhere between a few seconds to potentially hours, depending on the device's job queue. Devices in high demand typically have long wait times. Because of this wait time, Fire Opal is built such that the `execute` function will not block your program waiting for a result. Instead, you can ask Fire Opal to poll for the result and wait using the `.result()` command shown here,


```python
fire_opal_result = fire_opal_job.result()
```

Depending on the device queue times and the progress of your job, this cell may finish quickly or have to wait for a result. The status of your job may also be viewed on Amazon Braket, under [Quantum tasks](https://us-east-1.console.aws.amazon.com/braket/home?region=us-east-1#/tasks).

### Optional: Retrieve the results later

If you run a job and then close out of your notebook session, you may retrieve the job results later using the Fire Opal activity monitor and the `get_result` function. For more information, please see [How to view previous jobs and retrieve results](https://docs.q-ctrl.com/fire-opal/execute/submit-jobs/how-to-view-previous-jobs-and-retrieve-results.md).

## Step 6: Understand the results

Running a quantum circuit results in a collection of bitstrings and how often each was measured. This collection is best understood by plotting a histogram relating bitstrings to their frequency of occurrence. The following cell provides a few helpful plotting functions and then plots the results from above.


```python
plt.style.use(qv.get_qctrl_style())


def plot_bv_results(results, hidden_string, title="", max_columns=None):
    """
    Plot a probability histogram and highlight the hidden string.
    """

    # Restrict the number of columns displayed.
    if max_columns is not None:
        top_strings = sorted(results.keys(), key=lambda x: results[x], reverse=True)[
            :max_columns
        ]
        if hidden_string not in top_strings:
            top_strings.append(hidden_string)
        results = {s: results.get(s, 0) for s in top_strings}

    bitstrings = sorted(results.keys())

    def to_probability(value, total):
        if isinstance(value, float):
            return value
        return value / total

    probabilities = [to_probability(results[b], shot_count) for b in bitstrings]
    plt.figure(figsize=(20, 5))
    bars = plt.bar(bitstrings, probabilities)
    plt.xticks(rotation=90)

    for index, bitstring in enumerate(bitstrings):
        if bitstring != hidden_string:
            bars[index].set_color("grey")

    plt.ylabel("Probability")
    plt.ylim([0, 1])
    plt.title(title)
    plt.show()


def bitstring_count_to_probabilities(data, shot_count, number_of_counting_qubits):
    """
    Process the results.
    This function turns a dictionary of bitstring counts into a dictionary
    of probabilities.
    """
    probabilities = {
        format(int(bitstring, 2), f"0{number_of_counting_qubits+1}b"): (
            bitstring_count / shot_count
        )
        for bitstring, bitstring_count in data.items()
    }

    return probabilities


fire_opal_results = fire_opal_result["results"]
bitstring_results = {key[:-1]: value for key, value in fire_opal_results[0].items()}
if hidden_string not in bitstring_results:
    print("The hidden_string has 0% probability.")
else:
    print(f"Success probability: {100 * bitstring_results[hidden_string]:.2f}%")

plot_bv_results(
    bitstring_results,
    hidden_string=hidden_string,
    title="Fire Opal ($n=30$)",
    max_columns=100,
)
```

    Success probability: 99.28%



    
![png-6](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/png-6.webp)
    


Congratulations! 🔥 You have now successfully run a quantum program consisting of a 31 qubit Bernstein–Vazirani circuit.

## Optional: Compare Fire Opal to IonQ

If you are seeking a comparison, you may run the same program using IonQ, without realizing the error suppression benefits Fire Opal includes. The code below uses the same IonQ device as used previously to get a one-to-one comparison. Note that this job is also subject to the device queue and therefore may take anywhere from seconds to potentially hours.


```python
device = AwsDevice(arn="arn from selected device")
```


```python
from braket.ir.openqasm import Program as OpenQASMProgram

ionq_job = device.run(OpenQASMProgram(source=circuit_qasm), shots=shot_count)
```


```python
from braket.aws import AwsSession, AwsQuantumTask

# Restore task with unique arn
task_load = AwsQuantumTask("TASK_ID")
# Retrieve the result of the task
result = task_load.result()
counts = result.measurement_counts
aws_probabilities_ancilla = bitstring_count_to_probabilities(counts, shot_count, n)
aws_probabilities = {
    key[:-1]: value for key, value in aws_probabilities_ancilla.items()
}
```


```python
if hidden_string not in aws_probabilities:
    print("The hidden_string has 0% probability.")
else:
    print(f"Success probability: {100 * aws_probabilities[hidden_string]:.2f}%")

plot_bv_results(
    aws_probabilities,
    hidden_string=hidden_string,
    title=f"{device.name} ($n=31$)",
    max_columns=100,
)
```

    Success probability: 86.23%



    
![png-7](https://docs.q-ctrl.com/assets/img/notebooks/fire-opal/discover/start-using/get-started-with-fire-opal-on-ionq-through-amazon-braket/png-7.webp)
    


You may now compare this bitstring histogram and success probability to the previous one obtained when using Fire Opal.

## Show packages used to run this notebook

The package versions below were used to produce this notebook.


```python
from fireopal import print_package_versions

print_package_versions()
```

    | Package               | Version |
    | --------------------- | ------- |
    | Python                | 3.12.9  |
    | matplotlib            | 3.10.1  |
    | networkx              | 2.8.8   |
    | numpy                 | 1.26.4  |
    | sympy                 | 1.13.3  |
    | fire-opal             | 8.4.1   |
    | qctrl-visualizer      | 8.0.2   |
    | qctrl-workflow-client | 5.5.0   |
