Error due to insufficient memory in Amazon Braket
Amazon Braket Learning Course
I have created a learning course on Amazon Braket, AWS’s quantum computing service.
This course is designed for those with no prior knowledge of quantum computing or AWS, and by the end, you’ll even be able to learn about quantum machine learning. Take advantage of this opportunity to build your skills in quantum technologies!
Error Due to Insufficient Memory in Amazon Braket Local Simulator
When running the local simulator in Amazon Braket, AWS’s quantum computing service, the following error may be output.
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
Cell In[11], line 2
1 device = LocalSimulator()
----> 2 result = device.run(myqc, shots=1000).result()
3 counts = result.measurement_counts
4 plt.bar(counts.keys(), counts.values())
File ~/anaconda3/envs/Braket/lib/python3.10/site-packages/braket/devices/local_simulator.py:126, in LocalSimulator.run(self, task_specification, shots, inputs, *args, **kwargs)
124 task_specification = self._apply_noise_model_to_circuit(task_specification)
125 payload = self._construct_payload(task_specification, inputs, shots)
--> 126 result = self._delegate.run(payload, *args, shots=shots, **kwargs)
127 return LocalQuantumTask(self._to_result_object(result))
File ~/anaconda3/envs/Braket/lib/python3.10/site-packages/braket/default_simulator/simulator.py:149, in BaseLocalSimulator.run(self, circuit_ir, *args, **kwargs)
127 """
128 Simulate a circuit using either OpenQASM or Jaqcd.
129
(...)
146 are requested when shots>0.
147 """
148 if isinstance(circuit_ir, OpenQASMProgram):
--> 149 return self.run_openqasm(circuit_ir, *args, **kwargs)
150 return self.run_jaqcd(circuit_ir, *args, **kwargs)
File ~/anaconda3/envs/Braket/lib/python3.10/site-packages/braket/default_simulator/simulator.py:603, in BaseLocalSimulator.run_openqasm(self, openqasm_ir, shots, batch_size)
599 BaseLocalSimulator._validate_shots_and_ir_results(shots, circuit.results, qubit_count)
601 results = circuit.results
--> 603 simulation = self.initialize_simulation(
604 qubit_count=qubit_count, shots=shots, batch_size=batch_size
605 )
606 operations = circuit.instructions
607 simulation.evolve(operations)
File ~/anaconda3/envs/Braket/lib/python3.10/site-packages/braket/default_simulator/state_vector_simulator.py:41, in StateVectorSimulator.initialize_simulation(self, **kwargs)
39 shots = kwargs.get("shots")
40 batch_size = kwargs.get("batch_size")
---> 41 return StateVectorSimulation(qubit_count, shots, batch_size)
File ~/anaconda3/envs/Braket/lib/python3.10/site-packages/braket/default_simulator/state_vector_simulation.py:60, in StateVectorSimulation.__init__(self, qubit_count, shots, batch_size)
57 raise ValueError(f"batch_size must be a positive integer, but {batch_size} provided")
59 super().__init__(qubit_count=qubit_count, shots=shots)
---> 60 initial_state = np.zeros(2**qubit_count, dtype=complex)
61 initial_state[0] = 1
62 self._state_vector = initial_state
MemoryError: Unable to allocate 16.0 GiB for an array with shape (1073741824,) and data type complex128
This error occurs due to insufficient memory to execute the quantum circuit. In general, simulating an n-qubit system using a statevector simulator requires the following amount of memory:
$$ X^{Memory} = 2^n \times 16 \ Bytes. $$
Increasing the server’s memory size might make execution possible, but as shown in the above equation, even doubling the server’s memory only adds one quantum bit, which is not highly recommended.
Therefore, here are three suggestions to run without errors.
Run on Amazon Braket Managed Simulators
By using Amazon Braket managed simulators, you can run simulations with more qubits than on a local simulator. For example, the SV1 simulator supports simulations with up to 34 qubits, and the TN1 simulator supports up to 50 qubits. However, be aware that charges apply based on execution time, so use them with caution.
Run on an Actual Quantum Computer
Using a quantum computer that can handle a larger number of qubits allows for the execution of larger-scale calculations. For example, the IONQ Forte 1 can handle 36 qubits. However, be aware that the usage fees for quantum computers are not free, so pay attention to the costs.
Reduce the Number of Qubits in the Quantum Circuit
If you can reduce the number of qubits in the original quantum circuit, it can also be executed successfully.
Amazon Braket Learning Course
I have created a learning course on Amazon Braket, AWS’s quantum computing service.
This course is designed for those with no prior knowledge of quantum computing or AWS, and by the end, you’ll even be able to learn about quantum machine learning. Take advantage of this opportunity to build your skills in quantum technologies!