This paper intends to provide you with a general overview of the qengine framework by using the development of The Quantum Dice app for Android as a case study. The qengine framework describes the overall best practices and approach for implementing a quantum backend for use with Android devices.
Using the qengine framework is superior to other methods of implementing quantum computing on mobile devices because the qengine framework can create a circuit with pure qubits when required, whereas other methods display static information presented from quantum computers.
Before you get started
When developing with Qiskit, it is highly recommended you use the Anaconda distro of Python. At a very minimum, you must use Python3.
If you are using Android Studio, it is highly recommended you install the python plugin.
- Select File > Settings.
- Select Plugins page
- Click “Install JetBrains plugin”
- Select “Python Community Edition”
- click “Install”
- Restart Android Studio
Chaqopy Installation
Chaquopy is a plugin for the standard Android build system Gradle thereby allowing you to natively compile Python into your Android application. By including specific lines of code into your build files, you can implement Python natively into your applications.
Since Qiskit relies on Python proper usage of the Chaquopy plugin is essential to achieving quantum results on your mobile device.
If you intend to use your application for any purpose other than on your test environment, you’ll need to request a license from the Chaquopy developer. He’s a great guy, and super helpful if you ever get into issues with developing your application with Chaquopy.
1. Edit the App-Level build.gradle
Within the buildscript block, add the Chaquopy Maven repository and dependency to the end of the existing repositories and dependencies. Your build script should look like this:
buildscript { ext.kotlin_version = '1.3.20' // or most modern version repositories { google() jcenter() maven { url "https://chaquo.com/maven" } dependencies { classpath 'com.android.tools.build:gradle:3.4.1' // or most modernversion classpath "com.chaquo.python:gradle:6.2.2" // or most modern version classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
2. Edit the Module-Level build.gradle
Apply the Chaquopy plugin at the top of the file but after the Android plugin.
apply plugin: 'com.android.application' apply plugin: 'com.chaquo.python' // Python Integration apply plugin: ‘kotlin-android’ apply plugin: 'kotlin-android-extensions'
2.A Include Application Binary Interface Filter
You must pre-define the Application Binary Interface (ABI) [1] when Gradle builds your application. Guidance from Google states that all new applications must be compatible with ARM64.
The use of multiple ABIs with Chaquopy will increase the size of your application by 11mb per ABI [2].
When using all ABIs, your “abiFilters” line should look like this:
defaultConfig { ndk { abiFilters "x86", "armeabi-v7a", "arm64-v8a", "x86_64" }
2.B Integrate Python into Gradle
Set the Python executable using the “buildPython” setting. It is highly encouraged to use an Anaconda distro of Python; you must point directly to the location where the Anaconda Python 3 directory is, otherwise Chaquopy looks for the default python.
For example, on a Linux system, you might use the following:
defaultConfig { python { buildPython "/home/johndoe/anaconda3/bin/python" }
2.C Call Dependencies
Next, you need to define the pip dependencies used in your python script. Important to note is that Qiskit has a requirements specifier, this paper is tested only using Qiskit Version 0.7.0.
Your script should look like this:
defaultConfig { python { pip { install "numpy" install "qiskit==0.7.0" }
3. Include Code During Main.Activity Development
3.A Start it Up
It is essential to structure the app so that ‘Python .start()‘ is always located with an ‘AndroidPlatform‘ before attempting to run Python code.
The Quantum Dice app calls the python script only when the user presses the ‘roll dice’ button. To achieve this call and pass requirement, you must insert the following line of code into your main activity.
if (! Python.isStarted()) { Python.start(new AndroidPlatform(context))}
The Quantum Dice app’s call block looks like this:
if (! Python.isStarted()) { Python.start(AndroidPlatform(this)) val py: Python = Python.getInstance() var rawQ = py.getModule("qengine").callAttr("randint", xsides) var callQ = rawQ.toString() var qint = Integer.parseInt(callQ)
Once complete everything is good to begin developing.
Check out the github repo:
Or
Download the app to your Android Device:
References
- Android Documentation, ABI: https://developer.android.com/ndk/guides/abis
- Chaquo Documentation, FAQ: https://chaquo.com/chaquopy/faq/
1 thought on “3 Steps of The Qengine Framework”
Comments are closed.