Jump to content
Sign in to follow this  
lynkfs

Quantum computing with Smart

Recommended Posts

Is it possible to do a bit of quantum computing using SMS ?

The answer looks to be yes, using IBM's quantum computer resources which are partly made available for developers

Basics first : IBM has released it's SDK to program these quantum computers using a visual composer, Python, a proprietary assembler QASM format and some run and execute utilities.

There is however a second way of using its quantum resources, using the entry-points of is Q-experience REST server. Most if not all of the visual composer functions can be done by ajax-calls !

To demonstrate : the first thing is to acquire an api-token. 
(Create an account on https://quantumexperience.ng.bluemix.net/qx/experience. Log in and navigate to the Composer. Username > My Account, and then Advanced on the upper right. Then generate API Token. Api Tokens are personal and are not supposed to be shared.)

The next thing is to generate a session token (access token). This can be done in code.

This screenshot

 Capture.PNG

shows a couple of these REST server calls and its results :

  • The first button (get access) generates a session token
  • The second button queries which quantum computers are available, how many qubits can be used, if it is online and what type of system it is. Current results show 3 quantum systems - 2 real ones and 1 simulator
  • The third button (current Temp) gives some processor info of the selected system. In this case the current temperature in Kelvin : the Melbourne processor is cooled to just above the absolute minimum.
  • Next : the next button to implement will be to submit some code to one of the available processors for processing.

Below is the project code for these REST calls

unit Form1;

interface

uses 
  System.Types,
  System.Types.Convert,
  System.Objects,
  System.Time,
  SmartCL.System,
  SmartCL.Time,
  SmartCL.Graphics,
  SmartCL.Components,
  SmartCL.FileUtils,
  SmartCL.Forms,
  SmartCL.Fonts,
  SmartCL.Theme,
  SmartCL.Borders,
  SmartCL.Application, SmartCL.Net.Http, ECMA.Json, SmartCL.Controls.Button,
  SmartCL.Controls.Label, HTMLTableElement, SmartCL.Controls.ScrollBox,
  SmartCL.Controls.Panel, SmartCL.Controls.ComboBox;

type
  TForm1 = class(TW3Form)
    procedure W3Button3Click(Sender: TObject);
    procedure W3Button2Click(Sender: TObject);
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
    DBRows : integer := 0;
    FHttp : TW3HttpRequest;
    smscursor: variant;
    procedure GetAccessToken(Sender: TW3HttpRequest);
    procedure ListAvailableSystems(Sender: TW3HttpRequest);
    procedure GetTempKelvin(Sender: TW3HttpRequest);
    AccessToken : string;
  end;

implementation

{ TForm1 }

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;

{ Button1 }
procedure TForm1.W3Button1Click(Sender: TObject);
begin
//Get Access Token
  var FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := GetAccessToken;
  FHttp.open("POST",'https://quantumexperience.ng.bluemix.net/api/users/loginWithToken');
  FHttp.RequestHeaders.Add("Content-type","application/json");
  var api : variant := new JObject;
  api.apiToken := TString.encodeURIComponent('a59d26ebf........1d6d6d4a28c430b5336aba');
  FHttp.send(json.stringify(api));
end;

procedure TForm1.GetAccessToken(Sender: TW3HttpRequest);
begin

  smscursor := JSON.parse(Sender.ResponseText);
  AccessToken := smscursor.id;
  W3Label1.Caption:= 'Access Token : ' + AccessToken;
end;

{ Button2 }
procedure TForm1.W3Button2Click(Sender: TObject);
begin
//List available Q systems
  var FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := ListAvailableSystems;
  FHttp.open("GET",'https://quantumexperience.ng.bluemix.net/api/Backends?access_token=' + AccessToken);
  FHttp.RequestHeaders.Add("Content-type","application/json");
  FHttp.send;
end;

procedure TForm1.ListAvailableSystems(Sender: TW3HttpRequest);
begin

  var W3TableElement1 : TW3TableElement := TW3TableElement.Create(W3Panel1);

//add 5 columns to the grid
  W3TableElement1.AddColumn('Name',200);      //title, width
  W3TableElement1.AddColumn('Description',200);
  W3TableElement1.AddColumn('Qubits',50);
  W3TableElement1.AddColumn('System type',150);
  W3TableElement1.AddColumn('Status',50);

  smscursor := JSON.parse(Sender.ResponseText);

  W3ComboBox1.Clear;

  for var i := 0 to smscursor.length -1 do begin
    W3TableElement1.AddCell(i+1,1,smscursor[i].name);
    W3TableElement1.AddCell(i+1,2,smscursor[i].description);
    W3TableElement1.AddCell(i+1,3,smscursor[i].nQubits);
    if smscursor[i].simulator = true
      then W3TableElement1.AddCell(i+1,4,'quantum simulator')
      else W3TableElement1.AddCell(i+1,4,'quantum system');
    W3TableElement1.AddCell(i+1,5,smscursor[i].status);

    W3ComboBox1.add(smscursor[i].name);
  end;

  W3Panel1.NativeScrolling := true;

  W3ComboBox1.OnChanged := procedure (Sender: TObject)
  begin
    writeln(W3ComboBox1.Items[W3ComboBox1.SelectedIndex]);
  end;

end;

{ Button3 }
procedure TForm1.W3Button3Click(Sender: TObject);
begin
//Get Temp
  var FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := GetTempKelvin;
  //'https://quantumexperience.ng.bluemix.net/api/Backends/NAME/parameters?access_token=ACCESS-TOKEN';
  FHttp.open("GET",'https://quantumexperience.ng.bluemix.net/api/Backends/' +
                   W3ComboBox1.Items[W3ComboBox1.SelectedIndex] +
                   '/parameters?access_token=' +
                   AccessToken);
  FHttp.RequestHeaders.Add("Content-type","application/json");
  FHttp.send;
end;

procedure TForm1.GetTempKelvin(Sender: TW3HttpRequest);
begin

  If Sender.ResponseText = '{}' then
  begin
    W3Label3.Caption := 'Simulators are not cooled to (close to) absolute minimum';
  end else
  begin
    smscursor := JSON.parse(Sender.ResponseText);
    if smscursor.fridgeParameters.Temperature.value = ''
      then W3Label3.Caption := 'Processor does not support Temp reading'
      else W3Label3.Caption:= 'Current Temp : ' + smscursor.fridgeParameters.Temperature.value + ' Kelvin';
  end;
end;

procedure TForm1.Resize;
begin
  inherited;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

 

In the end hopefully it will be possible to recreate the classic Battleships game using quantum computing, as described here

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×