Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.

qiskitexamples: run examples on remote server & also on local simulator #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Sources/examples/CommandLineError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ import Foundation
*/
public enum CommandLineError: LocalizedError, CustomStringConvertible {

case missingToken
case invalidArgument(argument: String)
case missingOption
case invalidOption(option: String)
case invalidInput(input: String)

public var errorDescription: String? {
return self.description
}
public var description: String {
switch self {
case .missingToken:
return "Missing IBM Quantum Experience token."
case .invalidArgument(let argument):
return "Invalid argument \(argument)."
case .missingOption:
return "Missing option."
case .invalidOption(let option):
return "Invalid option \(option)."
case .invalidInput(let input):
return "Invalid input \(input)."
}
}
}
62 changes: 37 additions & 25 deletions Sources/examples/CommandLineHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class CommandLineHandler {
print("Options:")
print("--help Shows usage")
print("--token <token> Specifies IBM Quantum Experience Token")
print("--local Run examples on local simulator")
print("Input:")
print("None Runs all examples")
print("ghz|qft|rippleadd|teleport Runs specified example")
Expand All @@ -35,56 +36,67 @@ public final class CommandLineHandler {
public func run() throws {
guard arguments.count > 1 else {
CommandLineHandler.printUsage()
throw CommandLineError.missingToken
throw CommandLineError.missingOption
}
// The first argument is the execution path

var token: String?
var input = "all"

let argument = arguments[1].lowercased()
if argument == "--help" {
switch argument {
case "--help":
CommandLineHandler.printUsage()
return
}
guard argument == "--token" else {
CommandLineHandler.printUsage()
throw CommandLineError.invalidArgument(argument: argument)
}
guard arguments.count > 2 else {
case "--local":
if arguments.count > 2 {
input = arguments[2].lowercased()
}
case "--token":
guard arguments.count > 2 else {
CommandLineHandler.printUsage()
throw CommandLineError.missingOption
}
token = arguments[2]

if arguments.count > 3 {
input = arguments[3].lowercased()
}
default:
CommandLineHandler.printUsage()
throw CommandLineError.missingToken
}
let token = arguments[2]
var option: String = "all"
if arguments.count > 3 {
option = arguments[3].lowercased()
throw CommandLineError.invalidOption(option: argument)
}
switch option {

let option = CommandLineOption(apiToken: token)

switch input {
case "ghz":
GHZ.ghz(token) {
GHZ.ghz(option) {
print("*** Finished ***")
exit(0)
}
case "qft":
QFT.qft(token) {
QFT.qft(option) {
print("*** Finished ***")
exit(0)
}
case "rippleadd":
RippleAdd.rippleAdd(token) {
RippleAdd.rippleAdd(option) {
print("*** Finished ***")
exit(0)
}
case "teleport":
Teleport.teleport(token) {
Teleport.teleport(option) {
print("*** Finished ***")
exit(0)
}
case "all":
GHZ.ghz(token) {
GHZ.ghz(option) {
print("*** Finished ***")
QFT.qft(token) {
QFT.qft(option) {
print("*** Finished ***")
RippleAdd.rippleAdd(token) {
RippleAdd.rippleAdd(option) {
print("*** Finished ***")
Teleport.teleport(token) {
Teleport.teleport(option) {
print("*** Finished ***")
exit(0)
}
Expand All @@ -93,7 +105,7 @@ public final class CommandLineHandler {
}
default:
CommandLineHandler.printUsage()
throw CommandLineError.invalidOption(option: option)
throw CommandLineError.invalidInput(input: input)
}
RunLoop.main.run()
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/examples/CommandLineOption.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2018 IBM RESEARCH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

import Foundation

/**
Command Line Option
*/
public struct CommandLineOption {

enum Backend: String {
case ibmqx2 = "ibmqx2"
case ibmqxQasmSimulator = "ibmqx_qasm_simulator"
case localQasmSimulator = "local_qasm_simulator"
}

let apiToken: String?
}
Loading