├── tutorial ├── Cartfile ├── QisSwiftDeveloperTutorial.playground │ ├── Resources │ │ └── QISKit-c.gif │ └── contents.xcplayground ├── QuantumEmoticon.playground │ ├── contents.xcplayground │ └── Sources │ │ ├── String+BitString.swift │ │ └── NSView+TextField.swift └── VisualizingQuantumState.playground │ └── contents.xcplayground ├── images ├── QISKit-c.gif ├── QISKit.gif └── circuit_representations.png ├── Sources ├── crandom │ └── include │ │ ├── module.modulemap │ │ └── CRandom.h ├── cqasmparser │ ├── include │ │ └── module.modulemap │ └── ParseTree.c ├── qiskit │ ├── sdk │ │ ├── Gate.swift │ │ ├── CompositeGateComponent.swift │ │ ├── QisKitCompilerError.swift │ │ ├── Register.swift │ │ ├── InstructionSet.swift │ │ ├── Reset.swift │ │ ├── Measure.swift │ │ ├── QuantumRegister.swift │ │ ├── ClassicalRegister.swift │ │ ├── InstructionComponent.swift │ │ └── Instruction.swift │ ├── qasm │ │ ├── node │ │ │ ├── NodeRealValue.swift │ │ │ ├── NodeInclude.swift │ │ │ ├── NodeBarrier.swift │ │ │ ├── NodeReset.swift │ │ │ ├── NodeMeasure.swift │ │ │ ├── NodeInt.swift │ │ │ ├── NodeUniversalUnitary.swift │ │ │ ├── NodeCnot.swift │ │ │ ├── NodeProgram.swift │ │ │ ├── NodeIdList.swift │ │ │ ├── NodeExpressionList.swift │ │ │ ├── NodeMainProgram.swift │ │ │ ├── NodePrimaryList.swift │ │ │ ├── NodeMagic.swift │ │ │ ├── NodeIf.swift │ │ │ ├── NodeReal.swift │ │ │ ├── NodeGopList.swift │ │ │ ├── Node.swift │ │ │ ├── NodeQreg.swift │ │ │ ├── NodeCreg.swift │ │ │ ├── NodeIndexedId.swift │ │ │ ├── NodePrefix.swift │ │ │ ├── NodeGateBody.swift │ │ │ ├── NodeId.swift │ │ │ ├── NodeCustomUnitary.swift │ │ │ ├── NodeExternal.swift │ │ │ └── NodeBinaryOp.swift │ │ └── QasmError.swift │ ├── Info.plist │ ├── datastructures │ │ ├── String+Occurrences.swift │ │ ├── String+Base64.swift │ │ ├── String+ZFill.swift │ │ ├── Double+Format.swift │ │ ├── String+Padding.swift │ │ ├── Queue.swift │ │ ├── Matrix+ColumnRowValueArray.swift │ │ ├── Stack.swift │ │ ├── graph │ │ │ ├── GraphEdge.swift │ │ │ ├── GraphError.swift │ │ │ └── GraphVertex.swift │ │ ├── String+Random.swift │ │ ├── HashableTuple.swift │ │ └── RegBit.swift │ ├── tools │ │ ├── visualization │ │ │ ├── charts │ │ │ │ ├── StateQsphereChartSerie.swift │ │ │ │ ├── platforms │ │ │ │ │ └── AppleWebViewFactory.swift │ │ │ │ ├── BarChartConfiguration.swift │ │ │ │ └── StateQsphereChartValue.swift │ │ │ ├── VisualizationTypes.swift │ │ │ └── resources │ │ │ │ ├── echarts-4.0.4 │ │ │ │ └── LICENSE │ │ │ │ └── echarts-gl-1.1.0 │ │ │ │ └── LICENSE │ │ └── ToolsError.swift │ ├── dagcircuit │ │ ├── Layer.swift │ │ ├── CircuitEdgeData.swift │ │ └── GateData.swift │ ├── qiskit.h │ ├── mapper │ │ ├── CouplingVertexData.swift │ │ └── CouplingError.swift │ ├── extensions │ │ ├── Header.swift │ │ ├── CY.swift │ │ ├── CH.swift │ │ ├── CZ.swift │ │ ├── CX.swift │ │ ├── CXBase.swift │ │ ├── Swap.swift │ │ ├── CRZ.swift │ │ ├── CU1.swift │ │ └── Z.swift │ ├── backends │ │ ├── BaseBackend.swift │ │ ├── SimulatorError.swift │ │ └── QasmCppSimulator.swift │ ├── unroll │ │ └── BackendError.swift │ └── network │ │ ├── RequestTask.swift │ │ └── SessionDelegate.swift └── examples │ ├── main.swift │ └── CommandLineError.swift ├── Tests ├── qiskitTests │ ├── Info.plist │ └── qasm │ │ ├── example.swift │ │ ├── generic │ │ ├── qpt.swift │ │ ├── rb.swift │ │ ├── qft.swift │ │ ├── qec.swift │ │ ├── W-state.swift │ │ ├── teleport.swift │ │ ├── inverseqft2.swift │ │ ├── inverseqft1.swift │ │ ├── adder.swift │ │ ├── pea_3_pi_8.swift │ │ ├── ipea_3_pi_8.swift │ │ └── bigadder.swift │ │ ├── ibmqx2 │ │ ├── Deutsch_Algorithm.swift │ │ ├── iswap.swift │ │ ├── W3test.swift │ │ ├── qe_qft_3.swift │ │ ├── qe_qft_4.swift │ │ ├── 011_3_qubit_grover_50_.swift │ │ └── qe_qft_5.swift │ │ ├── plaquette_check.swift │ │ └── entangled_registers.swift └── LinuxMain.swift ├── .gitignore ├── Package.swift ├── CONTRIBUTING.rst └── qiskit.xcodeproj └── xcshareddata └── xcschemes ├── qiskitTests.xcscheme └── qiskitiOS.xcscheme /tutorial/Cartfile: -------------------------------------------------------------------------------- 1 | git "git@github.com:QISKit/qiskit-sdk-swift.git" "master" 2 | -------------------------------------------------------------------------------- /images/QISKit-c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiskit-community/qiskit-swift/HEAD/images/QISKit-c.gif -------------------------------------------------------------------------------- /images/QISKit.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiskit-community/qiskit-swift/HEAD/images/QISKit.gif -------------------------------------------------------------------------------- /Sources/crandom/include/module.modulemap: -------------------------------------------------------------------------------- 1 | module CRandom { 2 | header "CRandom.h" 3 | export * 4 | } -------------------------------------------------------------------------------- /Sources/cqasmparser/include/module.modulemap: -------------------------------------------------------------------------------- 1 | module CQasmParser { 2 | header "ParseTree.h" 3 | export * 4 | } 5 | -------------------------------------------------------------------------------- /images/circuit_representations.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiskit-community/qiskit-swift/HEAD/images/circuit_representations.png -------------------------------------------------------------------------------- /tutorial/QisSwiftDeveloperTutorial.playground/Resources/QISKit-c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiskit-community/qiskit-swift/HEAD/tutorial/QisSwiftDeveloperTutorial.playground/Resources/QISKit-c.gif -------------------------------------------------------------------------------- /tutorial/QuantumEmoticon.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /tutorial/VisualizingQuantumState.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /tutorial/QisSwiftDeveloperTutorial.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /tutorial/QuantumEmoticon.playground/Sources/String+BitString.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public extension String { 4 | init(bitSubstring: Substring) { 5 | self.init(bitstring: String(bitSubstring)) 6 | } 7 | 8 | init(bitstring: String) { 9 | let asciiInt = UInt8(strtoul(bitstring, nil, 2)) 10 | let scalar = UnicodeScalar(asciiInt) 11 | let char = Character(scalar) 12 | 13 | self.init(char) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tutorial/QuantumEmoticon.playground/Sources/NSView+TextField.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | 3 | extension NSView { 4 | public func addTextField(stringValue: String, 5 | fontName: String = "Courier", 6 | fontSize: CGFloat = 160.0, 7 | alphaValue: CGFloat) { 8 | let textField = NSTextField(frame: bounds) 9 | textField.stringValue = stringValue 10 | textField.font = NSFont(name: fontName, size: fontSize) 11 | textField.alphaValue = alphaValue 12 | 13 | addSubview(textField) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Tests/qiskitTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | .DS_Store 20 | *.xcworkspace 21 | 22 | #ignore Kiture packages 23 | Packages/ 24 | 25 | # CocoaPods 26 | # 27 | # We recommend against adding the Pods directory to your .gitignore. However 28 | # you should judge for yourself, the pros and cons are mentioned at: 29 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 30 | # 31 | PodFile.lock 32 | Pods/ 33 | 34 | Cartfile.resolved 35 | Carthage/ 36 | 37 | # Swift Package Manager 38 | .build/ 39 | 40 | ## MISC 41 | .swiftlint.yml 42 | *~ 43 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/Gate.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | User Defined Gate 20 | */ 21 | public protocol Gate: Instruction { 22 | } 23 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeRealValue.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | protocol NodeRealValue: Node { 20 | func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue 21 | } 22 | -------------------------------------------------------------------------------- /Sources/qiskit/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSHumanReadableCopyright 22 | Copyright © 2017 IBM. All rights reserved. 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/String+Occurrences.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | extension String { 19 | func occurrences(of char: Character) -> Int { 20 | return Array(self).reduce(0) { $0 + ($1 == char ? 1 : 0) } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/example.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | struct Example { 17 | static let QASM = """ 18 | OPENQASM 2.0; 19 | include "qelib1.inc"; 20 | qreg q[3]; 21 | qreg r[3]; 22 | h q; 23 | cx q, r; 24 | creg c[3]; 25 | creg d[3]; 26 | barrier q; 27 | measure q->c; 28 | measure r->d; 29 | """ 30 | } 31 | -------------------------------------------------------------------------------- /Sources/examples/main.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #if os(Linux) 17 | // do not show logging less than error 18 | import qiskit 19 | SDKLogger.type = .typeError 20 | #endif 21 | 22 | let clh = CommandLineHandler() 23 | 24 | do { 25 | try clh.run() 26 | } catch { 27 | print("An error occurred: \(error)") 28 | } 29 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/charts/StateQsphereChartSerie.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #if os(OSX) || os(iOS) 17 | 18 | // MARK: - Main body 19 | 20 | struct StateQsphereChartSerie { 21 | 22 | // MARK: - Public properties 23 | 24 | let probMix: Double 25 | let values: [StateQsphereChartValue] 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Sources/qiskit/dagcircuit/Layer.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | final class Layer { 19 | public let graph: DAGCircuit 20 | public let partition : [[RegBit]] 21 | 22 | init(_ graph: DAGCircuit, _ partition: [[RegBit]]) { 23 | self.graph = graph 24 | self.partition = partition 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/String+Base64.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | extension String { 19 | func base64Decoded() -> String? { 20 | guard let data = Data(base64Encoded: self) else { 21 | return nil 22 | } 23 | 24 | return String(data: data, encoding: .utf8) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/String+ZFill.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | extension String { 19 | public func zfill(_ length: Int) -> String { 20 | let diff = (length - count) 21 | let prefix = (diff > 0 ? String(repeating: "0", count: diff) : "") 22 | 23 | return (prefix + self) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/Double+Format.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | extension Double { 19 | func format(_ f: Int) -> String { 20 | return String(format: "%.\(f)f", self) 21 | } 22 | public func almostEqual(_ n: Double, _ delta: Double = 0.0000001) -> Bool { 23 | return (self-n).absolute() <= delta 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/qpt.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | struct QPT { 17 | static let QASM = """ 18 | OPENQASM 2.0; 19 | include "qelib1.inc"; 20 | gate pre q { } // pre-rotation 21 | gate post q { } // post-rotation 22 | qreg q[1]; 23 | creg c[1]; 24 | pre q[0]; 25 | barrier q; 26 | h q[0]; 27 | barrier q; 28 | post q[0]; 29 | measure q[0] -> c[0]; 30 | """ 31 | } 32 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/ibmqx2/Deutsch_Algorithm.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Implementation of Deutsch algorithm with two qubits for f(x)=x 17 | struct Deutsch { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | qreg q[5]; 23 | creg c[5]; 24 | 25 | x q[4]; 26 | h q[3]; 27 | h q[4]; 28 | cx q[3],q[4]; 29 | h q[3]; 30 | measure q[3] -> c[3]; 31 | """ 32 | } 33 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/String+Padding.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | extension String { 19 | func leftPadding(length: UInt, pad: String = " ") -> String { 20 | 21 | guard length > self.count else { return self } 22 | 23 | let padding = String(repeating: pad, count: Int(length) - self.count) 24 | return padding + self 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/ibmqx2/iswap.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Name of Experiment: iswap v4 17 | struct ISwap { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | qreg q[5]; 23 | creg c[5]; 24 | 25 | x q[0]; 26 | s q[0]; 27 | s q[1]; 28 | h q[0]; 29 | cx q[0],q[1]; 30 | h q[0]; 31 | h q[1]; 32 | cx q[0],q[1]; 33 | h q[0]; 34 | measure q[0] -> c[0]; 35 | measure q[1] -> c[1]; 36 | """ 37 | } 38 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/Queue.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | struct Queue { 19 | private var items = [Element]() 20 | 21 | var isEmpty: Bool { 22 | return self.items.isEmpty 23 | } 24 | mutating func enqueue(_ item: Element) { 25 | items.append(item) 26 | } 27 | mutating func dequeue() -> Element { 28 | return items.removeFirst() 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/Matrix+ColumnRowValueArray.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | extension Matrix { 17 | public func columnRowValueArray() -> [[Any]] { 18 | var result: [[Any]] = [] 19 | 20 | for row in 0.. 17 | 18 | //! Project version number for qiskit. 19 | FOUNDATION_EXPORT double qiskitVersionNumber; 20 | 21 | //! Project version string for qiskit. 22 | FOUNDATION_EXPORT const unsigned char qiskitVersionString[]; 23 | 24 | // In this header, you should import all the public headers of your framework using statements like #import 25 | 26 | 27 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/rb.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // One randomized benchmarking sequence 17 | struct RB { 18 | static let QASM: String = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | qreg q[2]; 22 | creg c[2]; 23 | h q[0]; 24 | barrier q; 25 | cz q[0],q[1]; 26 | barrier q; 27 | s q[0]; 28 | cz q[0],q[1]; 29 | barrier q; 30 | s q[0]; 31 | z q[0]; 32 | h q[0]; 33 | barrier q; 34 | measure q -> c; 35 | """ 36 | } 37 | -------------------------------------------------------------------------------- /Sources/qiskit/mapper/CouplingVertexData.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | final class CouplingVertexData: GraphDataCopying { 20 | let name: RegBit 21 | 22 | init(_ name: RegBit) { 23 | self.name = name 24 | } 25 | init(_ instance: CouplingVertexData) { 26 | self.name = instance.name 27 | } 28 | 29 | func copy() -> GraphDataCopying { 30 | return CouplingVertexData(self) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Sources/qiskit/dagcircuit/CircuitEdgeData.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | final class CircuitEdgeData: GraphDataCopying { 19 | public var name: RegBit 20 | 21 | public init(_ name: RegBit) { 22 | self.name = name 23 | } 24 | 25 | init(_ instance: CircuitEdgeData) { 26 | self.name = instance.name 27 | } 28 | 29 | func copy() -> GraphDataCopying { 30 | return CircuitEdgeData(self) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/qft.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // quantum Fourier transform 17 | struct QFT { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | qreg q[4]; 22 | creg c[4]; 23 | x q[0]; 24 | x q[2]; 25 | barrier q; 26 | h q[0]; 27 | cu1(pi/2) q[1],q[0]; 28 | h q[1]; 29 | cu1(pi/4) q[2],q[0]; 30 | cu1(pi/2) q[2],q[1]; 31 | h q[2]; 32 | cu1(pi/8) q[3],q[0]; 33 | cu1(pi/4) q[3],q[1]; 34 | cu1(pi/2) q[3],q[2]; 35 | h q[3]; 36 | measure q -> c; 37 | """ 38 | } 39 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/Stack.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | struct Stack { 19 | private(set) var items = [Element]() 20 | 21 | var isEmpty: Bool { 22 | return self.items.isEmpty 23 | } 24 | mutating func push(_ item: Element) { 25 | items.append(item) 26 | } 27 | @discardableResult 28 | mutating func pop() -> Element { 29 | return items.removeLast() 30 | } 31 | func peek() -> Element? { 32 | return items.last 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/plaquette_check.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // plaquette check 17 | struct PlaquetteCheck { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | qreg q[5]; 23 | creg c[5]; 24 | 25 | x q[1]; 26 | x q[4]; 27 | barrier q; 28 | 29 | cx q[0], q[2]; 30 | cx q[1], q[2]; 31 | cx q[3], q[2]; 32 | cx q[4], q[2]; 33 | barrier q; 34 | measure q[0]->c[0]; 35 | measure q[1]->c[1]; 36 | measure q[2]->c[2]; 37 | measure q[3]->c[3]; 38 | measure q[4]->c[4]; 39 | """ 40 | } 41 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeInclude.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | final class NodeInclude: Node { 20 | 21 | let file: String 22 | 23 | init(file: String) { 24 | self.file = file 25 | } 26 | 27 | var type: NodeType { 28 | return .N_INCLUDE 29 | } 30 | 31 | var children: [Node] { 32 | return [] 33 | } 34 | 35 | func qasm(_ prec: Int) -> String { 36 | let qasm: String = "include \(file);" 37 | return qasm 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Sources/crandom/include/CRandom.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #ifndef CRandom_h 17 | #define CRandom_h 18 | 19 | extern const int N; 20 | 21 | struct CRandomState { 22 | unsigned long *mt; /* the array for the state vector */ 23 | int mti; /* mti==N+1 means mt[N] is not initialized */ 24 | }; 25 | 26 | extern void init_by_array(struct CRandomState *pState,unsigned long init_key[], int key_length); 27 | extern unsigned long genrand_int32(struct CRandomState *pState); 28 | extern double genrand_res53(struct CRandomState *pState); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/qec.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Repetition code syndrome measurement 17 | struct QEC { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | qreg q[3]; 22 | qreg a[2]; 23 | creg c[3]; 24 | creg syn[2]; 25 | gate syndrome d1,d2,d3,a1,a2 26 | { 27 | cx d1,a1; cx d2,a1; 28 | cx d2,a2; cx d3,a2; 29 | } 30 | x q[0]; // error 31 | barrier q; 32 | syndrome q[0],q[1],q[2],a[0],a[1]; 33 | measure a -> syn; 34 | if(syn==1) x q[0]; 35 | if(syn==2) x q[2]; 36 | if(syn==3) x q[1]; 37 | measure q -> c; 38 | """ 39 | } 40 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/entangled_registers.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // A simple 8 qubit example entangling two 4 qubit registers 17 | struct EntangledRegisters { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | qreg a[4]; 23 | qreg b[4]; 24 | creg c[4]; 25 | creg d[4]; 26 | h a; 27 | cx a, b; 28 | barrier a; 29 | barrier b; 30 | measure a[0]->c[0]; 31 | measure a[1]->c[1]; 32 | measure a[2]->c[2]; 33 | measure a[3]->c[3]; 34 | measure b[0]->d[0]; 35 | measure b[1]->d[1]; 36 | measure b[2]->d[2]; 37 | measure b[3]->d[3]; 38 | """ 39 | } 40 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/W-state.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Name of Experiment: W-state v1 17 | struct WState { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | 23 | qreg q[3]; 24 | creg c[3]; 25 | gate cH a,b { 26 | h b; 27 | sdg b; 28 | cx a,b; 29 | h b; 30 | t b; 31 | cx a,b; 32 | t b; 33 | h b; 34 | s b; 35 | x b; 36 | s a; 37 | } 38 | 39 | u3(1.910630000000000,0,0) q[0]; 40 | cH q[0],q[1]; 41 | ccx q[0],q[1],q[2]; 42 | x q[0]; 43 | x q[1]; 44 | cx q[0],q[1]; 45 | measure q[0] -> c[0]; 46 | measure q[1] -> c[1]; 47 | measure q[2] -> c[2]; 48 | """ 49 | } 50 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/Header.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | public final class Header: QuantumCircuitHeader { 19 | 20 | override public var value: String { 21 | return "\(super.value)\ninclude \"qelib1.inc\";" 22 | } 23 | 24 | override public func copy() -> QuantumCircuitHeader { 25 | return Header() 26 | } 27 | } 28 | 29 | extension QuantumCircuit { 30 | convenience public init() { 31 | self.init(Header()) 32 | } 33 | convenience public init(_ regs: [Register]) throws { 34 | try self.init(regs, Header()) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeBarrier.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | /* 19 | class Barrier(Node): 20 | Node for an OPENQASM barrier statement. 21 | children[0] is a primarylist node. 22 | */ 23 | final class NodeBarrier: Node { 24 | 25 | let list: Node 26 | 27 | init(list: Node) { 28 | self.list = list 29 | } 30 | 31 | var type: NodeType { 32 | return .N_BARRIER 33 | } 34 | 35 | var children: [Node] { 36 | return [self.list] 37 | } 38 | 39 | func qasm(_ prec: Int) -> String { 40 | return "barrier \(self.list.qasm(prec));" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/ibmqx2/W3test.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Name of Experiment: W3test v1 17 | // example of W-state |001> + |010> + |100> 18 | // found by user lukasknips at http://ibm.biz/qiskit-W3-Example 19 | struct W3Test { 20 | static let QASM = """ 21 | OPENQASM 2.0; 22 | include "qelib1.inc"; 23 | 24 | 25 | qreg q[5]; 26 | creg c[5]; 27 | 28 | u3(-1.230960000000000,0,0) q[0]; 29 | u3(pi/4,0,0) q[1]; 30 | cx q[0],q[2]; 31 | z q[2]; 32 | h q[2]; 33 | cx q[1],q[2]; 34 | z q[2]; 35 | u3(pi/4,0,0) q[1]; 36 | h q[2]; 37 | cx q[1],q[2]; 38 | measure q[0] -> c[0]; 39 | measure q[1] -> c[1]; 40 | measure q[2] -> c[2]; 41 | """ 42 | } 43 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeReset.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Node for an OPENQASM reset statement. 20 | children[0] is a primary node (id or indexedid) 21 | */ 22 | final class NodeReset: Node { 23 | 24 | let indexedid: Node 25 | 26 | init(indexedid: Node) { 27 | self.indexedid = indexedid 28 | } 29 | 30 | var type: NodeType { 31 | return .N_RESET 32 | } 33 | 34 | var children: [Node] { 35 | return [self.indexedid] 36 | } 37 | 38 | func qasm(_ prec: Int) -> String { 39 | return "reset \(self.indexedid.qasm(prec));" 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/teleport.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // quantum teleportation example 17 | struct Teleport { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | qreg q[3]; 22 | creg c0[1]; 23 | creg c1[1]; 24 | creg c2[1]; 25 | // optional post-rotation for state tomography 26 | gate post q { } 27 | u3(0.300000000000000,0.200000000000000,0.100000000000000) q[0]; 28 | h q[1]; 29 | cx q[1],q[2]; 30 | barrier q; 31 | cx q[0],q[1]; 32 | h q[0]; 33 | measure q[0] -> c0[0]; 34 | measure q[1] -> c1[0]; 35 | if(c0==1) z q[2]; 36 | if(c1==1) x q[2]; 37 | post q[2]; 38 | measure q[2] -> c2[0]; 39 | """ 40 | } 41 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/inverseqft2.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // QFT and measure, version 2 17 | struct InversQFT2 { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | qreg q[4]; 22 | creg c0[1]; 23 | creg c1[1]; 24 | creg c2[1]; 25 | creg c3[1]; 26 | h q; 27 | barrier q; 28 | h q[0]; 29 | measure q[0] -> c0[0]; 30 | if(c0==1) u1(pi/2) q[1]; 31 | h q[1]; 32 | measure q[1] -> c1[0]; 33 | if(c0==1) u1(pi/4) q[2]; 34 | if(c1==1) u1(pi/2) q[2]; 35 | h q[2]; 36 | measure q[2] -> c2[0]; 37 | if(c0==1) u1(pi/8) q[3]; 38 | if(c1==1) u1(pi/4) q[3]; 39 | if(c2==1) u1(pi/2) q[3]; 40 | h q[3]; 41 | measure q[3] -> c3[0]; 42 | """ 43 | } 44 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/CompositeGateComponent.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Composite gate component 20 | */ 21 | public final class CompositeGateComponent { 22 | 23 | var data: [Instruction] = [] // gate sequence defining the composite unitary 24 | var inverse_flag = false 25 | 26 | init() { 27 | } 28 | 29 | func copy(_ c: QuantumCircuit) -> CompositeGateComponent { 30 | let cg = CompositeGateComponent() 31 | cg.inverse_flag = self.inverse_flag 32 | for instruction in self.data { 33 | cg.data.append((instruction as! CopyableInstruction).copy(c)) 34 | } 35 | return cg 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/QisKitCompilerError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Exceptions raised during compilation 20 | */ 21 | public enum QisKitCompilerError: LocalizedError, CustomStringConvertible { 22 | 23 | case unknownFormat(name: String) 24 | case internalError(error: Error) 25 | 26 | public var errorDescription: String? { 27 | return self.description 28 | } 29 | public var description: String { 30 | switch self { 31 | case .unknownFormat(let name): 32 | return "Unrecognized circuit format '\(name)'" 33 | case .internalError(let error): 34 | return error.localizedDescription 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/graph/GraphEdge.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | final class GraphEdge { 19 | 20 | public var data: EdgeDataType? = nil 21 | public let source: Int 22 | public let neighbor: Int 23 | 24 | init(_ source: Int, _ neighbor: Int) { 25 | self.source = source 26 | self.neighbor = neighbor 27 | } 28 | 29 | public func copy() -> GraphEdge { 30 | let copy = GraphEdge(self.source,self.neighbor) 31 | if self.data != nil { 32 | let d = self.data!.copy() as! EdgeDataType 33 | copy.data = d 34 | } 35 | return copy 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/graph/GraphError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Graph Exceptions 20 | */ 21 | public enum GraphError: LocalizedError, CustomStringConvertible { 22 | 23 | case isUndirected 24 | case isCyclic 25 | case connectEmptyGraph 26 | 27 | public var errorDescription: String? { 28 | return self.description 29 | } 30 | public var description: String { 31 | switch self { 32 | case .isUndirected: 33 | return "Graph is undirected" 34 | case .isCyclic: 35 | return "Graph has cycles" 36 | case .connectEmptyGraph: 37 | return "Connectivity is undefined for the null graph." 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeMeasure.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | /* 19 | Node for an OPENQASM measure statement. 20 | children[0] is a primary node (id or indexedid) 21 | children[1] is a primary node (id or indexedid) 22 | */ 23 | final class NodeMeasure: Node { 24 | 25 | let arg1: Node 26 | let arg2: Node 27 | 28 | init(arg1: Node, arg2: Node) { 29 | self.arg1 = arg1 30 | self.arg2 = arg2 31 | } 32 | 33 | var type: NodeType { 34 | return .N_MEASURE 35 | } 36 | 37 | var children: [Node] { 38 | return [arg1,arg2] 39 | } 40 | 41 | func qasm(_ prec: Int) -> String { 42 | return "measure \(arg1.qasm(prec)) -> \(arg2.qasm(prec));" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeInt.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM integer. 21 | This node has no children. The data is in the value field. 22 | */ 23 | final class NodeNNInt: NodeRealValue { 24 | 25 | let value: Int 26 | 27 | init(value: Int) { 28 | self.value = value 29 | } 30 | 31 | var type: NodeType { 32 | return .N_INT 33 | } 34 | 35 | var children: [Node] { 36 | return [] 37 | } 38 | 39 | func qasm(_ prec: Int) -> String { 40 | let qasm: String = "\(value)" 41 | return qasm 42 | } 43 | 44 | func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue { 45 | return SymbolicValue(Double(self.value)) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/inverseqft1.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // QFT and measure, version 1 17 | struct InverseQFT1 { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | qreg q[4]; 22 | creg c[4]; 23 | h q; 24 | barrier q; 25 | h q[0]; 26 | measure q[0] -> c[0]; 27 | if(c==1) u1(pi/2) q[1]; 28 | h q[1]; 29 | measure q[1] -> c[1]; 30 | if(c==1) u1(pi/4) q[2]; 31 | if(c==2) u1(pi/2) q[2]; 32 | if(c==3) u1(pi/2+pi/4) q[2]; 33 | h q[2]; 34 | measure q[2] -> c[2]; 35 | if(c==1) u1(pi/8) q[3]; 36 | if(c==2) u1(pi/4) q[3]; 37 | if(c==3) u1(pi/4+pi/8) q[3]; 38 | if(c==4) u1(pi/2) q[3]; 39 | if(c==5) u1(pi/2+pi/8) q[3]; 40 | if(c==6) u1(pi/2+pi/4) q[3]; 41 | if(c==7) u1(pi/2+pi/4+pi/8) q[3]; 42 | h q[3]; 43 | measure q[3] -> c[3]; 44 | """ 45 | } 46 | -------------------------------------------------------------------------------- /Sources/examples/CommandLineError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Command Line Exceptions 20 | */ 21 | public enum CommandLineError: LocalizedError, CustomStringConvertible { 22 | 23 | case missingToken 24 | case invalidArgument(argument: String) 25 | case invalidOption(option: String) 26 | 27 | public var errorDescription: String? { 28 | return self.description 29 | } 30 | public var description: String { 31 | switch self { 32 | case .missingToken: 33 | return "Missing IBM Quantum Experience token." 34 | case .invalidArgument(let argument): 35 | return "Invalid argument \(argument)." 36 | case .invalidOption(let option): 37 | return "Invalid option \(option)." 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeUniversalUnitary.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | /* 18 | Node for an OPENQASM U statement. 19 | children[0] is an expressionlist node. 20 | children[1] is a primary node (id or indexedid). 21 | */ 22 | final class NodeUniversalUnitary: Node { 23 | 24 | let explist: Node 25 | let indexedid: Node 26 | 27 | init(explist: Node, indexedid: Node) { 28 | self.explist = explist 29 | self.indexedid = indexedid 30 | } 31 | 32 | var type: NodeType { 33 | return .N_UNIVERSALUNITARY 34 | } 35 | 36 | var children: [Node] { 37 | return [self.explist,self.indexedid] 38 | } 39 | 40 | func qasm(_ prec: Int) -> String { 41 | return "U (\(self.explist.qasm(prec))) \(self.indexedid.qasm(prec));" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:4.0 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "qiskit", 8 | products: [ 9 | // Products define the executables and libraries produced by a package, and make them visible to other packages. 10 | .library( 11 | name: "qiskit", 12 | targets: ["qiskit"]), 13 | .executable( 14 | name: "qiskitexamples", targets: ["examples"]), 15 | ], 16 | dependencies: [ 17 | // Dependencies declare other packages that this package depends on. 18 | // .package(url: /* package url */, from: "1.0.0"), 19 | ], 20 | targets: [ 21 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 22 | // Targets can depend on other targets in this package, and on products in packages which this package depends on. 23 | .target( 24 | name: "crandom", 25 | dependencies: []), 26 | .target( 27 | name: "cqasmparser", 28 | dependencies: []), 29 | .target( 30 | name: "qiskit", 31 | dependencies: ["crandom","cqasmparser"]), 32 | .target( 33 | name: "examples", 34 | dependencies: ["qiskit"]), 35 | .testTarget( 36 | name: "qiskitTests", 37 | dependencies: ["qiskit"]), 38 | ], 39 | swiftLanguageVersions: [4] 40 | ) 41 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/String+Random.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | extension String { 19 | static func randomAlphanumeric(length: Int) -> String { 20 | struct StaticVars { 21 | static let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 22 | static let lettersLength = UInt32(letters.count) 23 | } 24 | var array = [Character](repeating: " ", count: length) 25 | let random = Random(time(nil)) 26 | for i in 0.. 19 | 20 | public struct HashableTuple : Hashable, CustomStringConvertible { 21 | public let one: A 22 | public let two: B 23 | 24 | public init(_ one: A, _ two: B) { 25 | self.one = one 26 | self.two = two 27 | } 28 | public var hashValue : Int { 29 | get { 30 | return self.one.hashValue &* 31 &+ self.two.hashValue 31 | } 32 | } 33 | 34 | public var description: String { 35 | return "(\(self.one), \(self.two))" 36 | } 37 | 38 | public static func ==(lhs: HashableTuple, rhs: HashableTuple) -> Bool { 39 | return lhs.one == rhs.one && lhs.two == rhs.two 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/VisualizationTypes.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #if os(OSX) || os(iOS) 17 | 18 | #if os(OSX) 19 | 20 | import Cocoa 21 | 22 | #else 23 | 24 | import UIKit 25 | 26 | #endif 27 | 28 | // MARK: - Public body 29 | 30 | public struct VisualizationTypes { 31 | 32 | // MARK: - Types 33 | 34 | #if os(OSX) 35 | 36 | public typealias View = NSView 37 | 38 | #else 39 | 40 | public typealias View = UIView 41 | 42 | #endif 43 | 44 | public struct Size { 45 | 46 | // MARK: - Public properties 47 | 48 | let width: Int 49 | let height: Int 50 | 51 | // MARK: - Init methods 52 | 53 | public init(width: Int, height: Int) { 54 | self.width = width 55 | self.height = height 56 | } 57 | } 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /Sources/qiskit/dagcircuit/GateData.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | "opaque" = True or False 20 | "n_args" = number of real parameters 21 | "n_bits" = number of qubits 22 | "args" = list of parameter names 23 | "bits" = list of qubit names 24 | "body" = GateBody AST node 25 | */ 26 | 27 | final class GateData { 28 | 29 | let opaque: Bool 30 | let n_args: Int 31 | let n_bits: Int 32 | let args: [String] 33 | let bits: [String] 34 | let body: NodeGateBody? 35 | 36 | init(_ opaque: Bool, _ n_args: Int, _ n_bits: Int, _ args: [String], _ bits: [String], _ body: NodeGateBody?) { 37 | self.opaque = opaque 38 | self.n_args = n_args 39 | self.n_bits = n_bits 40 | self.args = args 41 | self.bits = bits 42 | self.body = body 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/charts/platforms/AppleWebViewFactory.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #if os(OSX) || os(iOS) 17 | 18 | import WebKit 19 | 20 | // MARK: - Main body 21 | 22 | struct AppleWebViewFactory { 23 | 24 | // MARK: - Public class methods 25 | 26 | static func makeWebView(size: VisualizationTypes.Size, html: String) -> VisualizationTypes.View { 27 | let width = CGFloat(size.width) 28 | let height = CGFloat(size.height) 29 | 30 | #if os(OSX) 31 | 32 | let frame = NSMakeRect(0, 0, width, height) 33 | 34 | #else 35 | 36 | let frame = CGRect(x: 0, y: 0, width: width, height: height) 37 | 38 | #endif 39 | 40 | let webview = WKWebView(frame: frame) 41 | 42 | webview.loadHTMLString(html, baseURL: nil) 43 | 44 | return webview 45 | } 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import XCTest 17 | @testable import qiskitTests 18 | import qiskit 19 | 20 | // do not show logging less than error 21 | SDKLogger.type = .typeError 22 | 23 | XCTMain([ 24 | testCase(AuthenticationTests.allTests), 25 | testCase(DataStructureTests.allTests), 26 | testCase(HamiltonianTests.allTests), 27 | testCase(IBMQuantumExperienceTests.allTests), 28 | testCase(LocalQasmSimulatorTests.allTests), 29 | testCase(LocalUnitarySimulatorTests.allTests), 30 | testCase(PauliTests.allTests), 31 | testCase(QiskitParserTests.allTests), 32 | testCase(QiskitProgramTests.allTests), 33 | testCase(QiskitTests.allTests), 34 | testCase(QITests.allTests), 35 | testCase(QuantumOptimizationTests.allTests), 36 | testCase(QuantumProgramTests.allTests), 37 | testCase(UnrollerTests.allTests) 38 | ]) 39 | 40 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeCnot.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM CNOT statement. 21 | children[0], children[1] are id nodes if CX is inside a gate body, 22 | otherwise they are primary nodes. 23 | */ 24 | final class NodeCnot: Node { 25 | 26 | let arg1: Node 27 | let arg2: Node 28 | 29 | init(arg1: Node, arg2: Node) { 30 | self.arg1 = arg1 31 | self.arg2 = arg2 32 | } 33 | 34 | var type: NodeType { 35 | return .N_CNOT 36 | } 37 | 38 | var children: [Node] { 39 | return [self.arg1,self.arg2] 40 | } 41 | 42 | func qasm(_ prec: Int) -> String { 43 | var qasm: String = "CX" 44 | qasm += " \(self.arg1.qasm(prec))" 45 | qasm += ", \(self.arg2.qasm(prec))" 46 | qasm += ";" 47 | return qasm 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeProgram.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Node for an OPENQASM program. 20 | children is a list of nodes (statements). 21 | */ 22 | final class NodeProgram: Node { 23 | 24 | private(set) var statements: [Node] 25 | 26 | init(statement: Node) { 27 | self.statements = [statement] 28 | } 29 | 30 | func addStatement(statement: Node) { 31 | self.statements.append(statement) 32 | } 33 | 34 | var type: NodeType { 35 | return .N_PROGRAM 36 | } 37 | 38 | var children: [Node] { 39 | return self.statements 40 | } 41 | 42 | func qasm(_ prec: Int) -> String { 43 | let qasms: [String] = self.statements.compactMap({ (node: Node) -> String in 44 | return node.qasm(prec) 45 | }) 46 | return qasms.joined(separator: "\n") 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeIdList.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM idlist. 21 | children is a list of id nodes. 22 | */ 23 | final class NodeIdList: Node { 24 | 25 | private(set) var identifiers: [Node] 26 | 27 | init(identifier: Node) { 28 | self.identifiers = [identifier] 29 | } 30 | 31 | func addIdentifier(identifier: Node) { 32 | self.identifiers.append(identifier) 33 | } 34 | 35 | var type: NodeType { 36 | return .N_IDLIST 37 | } 38 | 39 | var children: [Node] { 40 | return self.identifiers 41 | } 42 | 43 | func qasm(_ prec: Int) -> String { 44 | let qasms: [String] = self.identifiers.compactMap({ (node: Node) -> String in 45 | return node.qasm(prec) 46 | }) 47 | return qasms.joined(separator: ",") 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Sources/qiskit/backends/BaseBackend.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | This module implements the abstract base class for backend modules. 20 | To create add-on backend modules subclass the Backend class in this module. 21 | Doing so requires that the required backend interface is implemented. 22 | */ 23 | public class BaseBackend { 24 | 25 | public var configuration : [String:Any] { 26 | return self._configuration 27 | } 28 | var _configuration : [String:Any] = [:] 29 | 30 | public required init(_ configuration: [String:Any]? = nil) { 31 | if type(of: self) == BaseBackend.self { 32 | fatalError("Abstract class instantiation.") 33 | } 34 | } 35 | 36 | @discardableResult 37 | public func run(_ q_job: QuantumJob, response: @escaping ((_:Result) -> Void)) -> RequestTask { 38 | preconditionFailure("run not implemented") 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeExpressionList.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM expression list. 21 | children are expression nodes. 22 | */ 23 | final class NodeExpressionList: Node { 24 | 25 | private(set) var expressionList: [Node] 26 | 27 | init(expression: Node) { 28 | self.expressionList = [expression] 29 | } 30 | 31 | func addExpression(exp: Node) { 32 | self.expressionList.insert(exp, at: 0) 33 | } 34 | 35 | var type: NodeType { 36 | return .N_EXPRESSIONLIST 37 | } 38 | 39 | var children: [Node] { 40 | return self.expressionList 41 | } 42 | 43 | func qasm(_ prec: Int) -> String { 44 | var qasms: [String] = [] 45 | for node in self.expressionList { 46 | qasms.append(node.qasm(prec)) 47 | } 48 | return qasms.joined(separator: ",") 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/resources/echarts-4.0.4/LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, Baidu Inc. 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/resources/echarts-gl-1.1.0/LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, Baidu Inc. 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeMainProgram.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | final class NodeMainProgram: Node { 20 | 21 | let magic: Node 22 | let incld: Node? 23 | let program: Node 24 | 25 | init(magic: Node, program: Node) { 26 | self.magic = magic 27 | self.incld = nil 28 | self.program = program 29 | } 30 | 31 | init(magic: Node, incld: Node, program: Node) { 32 | self.magic = magic 33 | self.incld = incld 34 | self.program = program 35 | } 36 | 37 | var type: NodeType { 38 | return .N_MAINPROGRAM 39 | } 40 | 41 | var children: [Node] { 42 | return [] 43 | } 44 | 45 | func qasm(_ prec: Int) -> String { 46 | var qasm: String = self.magic.qasm(prec) 47 | qasm += "\(self.incld?.qasm(prec) ?? "")\n" 48 | qasm += "\(self.program.qasm(prec))\n" 49 | return qasm 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodePrimaryList.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Node for an OPENQASM primarylist. 20 | children is a list of primary nodes. Primary nodes are indexedid or id. 21 | */ 22 | final class NodePrimaryList: Node { 23 | 24 | private(set) var identifiers: [Node] 25 | 26 | init(identifier: Node) { 27 | self.identifiers = [identifier] 28 | } 29 | 30 | func addIdentifier(identifier: Node) { 31 | self.identifiers.append(identifier) 32 | } 33 | 34 | var type: NodeType { 35 | return .N_PRIMARYLIST 36 | } 37 | 38 | var children: [Node] { 39 | return self.identifiers 40 | } 41 | 42 | func qasm(_ prec: Int) -> String { 43 | let qasms: [String] = self.identifiers.compactMap({ (node: Node) -> String in 44 | return node.qasm(prec) 45 | }) 46 | return qasms.joined(separator: ",") 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Sources/qiskit/mapper/CouplingError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /** 20 | Exception for errors raised by the Coupling object. 21 | */ 22 | public enum CouplingError: LocalizedError, CustomStringConvertible { 23 | case duplicateregbit(regBit: RegBit) 24 | case notconnected 25 | case distancenotcomputed 26 | case notincouplinggraph(regBit: RegBit) 27 | 28 | public var errorDescription: String? { 29 | return self.description 30 | } 31 | public var description: String { 32 | switch self { 33 | case .duplicateregbit(let regBit): 34 | return "'\(regBit.description)' already in coupling graph" 35 | case .notconnected: 36 | return "coupling graph not connected" 37 | case .distancenotcomputed: 38 | return "distance has not been computed" 39 | case .notincouplinggraph(let regBit): 40 | return "'\(regBit.description)' not in coupling graph" 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeMagic.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM file identifier/version statement ("magic number"). 21 | children[0] is a floating point number (not a node). 22 | */ 23 | final class NodeMagic: Node { 24 | 25 | let nodeVersion: NodeReal? 26 | 27 | init(version: Node) { 28 | self.nodeVersion = (version as? NodeReal) 29 | } 30 | 31 | var type: NodeType { 32 | return .N_MAGIC 33 | } 34 | 35 | var children: [Node] { 36 | var _children: [Node] = [] 37 | 38 | if let version = nodeVersion { 39 | _children.append(version) 40 | } 41 | 42 | return _children 43 | } 44 | 45 | func qasm(_ prec: Int) -> String { 46 | guard let version = nodeVersion else { 47 | assertionFailure("Invalid NodeMagic Operation") 48 | return "" 49 | } 50 | return "OPENQASM \(version.value.format(1));" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/adder.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // quantum ripple-carry adder from Cuccaro et al, quant-ph/0410184 17 | struct Adder { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | gate majority a,b,c 22 | { 23 | cx c,b; 24 | cx c,a; 25 | ccx a,b,c; 26 | } 27 | gate unmaj a,b,c 28 | { 29 | ccx a,b,c; 30 | cx c,a; 31 | cx a,b; 32 | } 33 | qreg cin[1]; 34 | qreg a[4]; 35 | qreg b[4]; 36 | qreg cout[1]; 37 | creg ans[5]; 38 | // set input states 39 | x a[0]; // a = 0001 40 | x b; // b = 1111 41 | // add a to b, storing result in b 42 | majority cin[0],b[0],a[0]; 43 | majority a[0],b[1],a[1]; 44 | majority a[1],b[2],a[2]; 45 | majority a[2],b[3],a[3]; 46 | cx a[3],cout[0]; 47 | unmaj a[2],b[3],a[3]; 48 | unmaj a[1],b[2],a[2]; 49 | unmaj a[0],b[1],a[1]; 50 | unmaj cin[0],b[0],a[0]; 51 | measure b[0] -> ans[0]; 52 | measure b[1] -> ans[1]; 53 | measure b[2] -> ans[2]; 54 | measure b[3] -> ans[3]; 55 | measure cout[0] -> ans[4]; 56 | """ 57 | } 58 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/pea_3_pi_8.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Name of Experiment: pea_3*pi/8 v3 17 | struct Pea3pi8 { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | 23 | qreg q[5]; 24 | creg c[4]; 25 | gate cu1fixed (a) c,t { 26 | u1 (-a) t; 27 | cx c,t; 28 | u1 (a) t; 29 | cx c,t; 30 | } 31 | gate cu c,t { 32 | cu1fixed (3*pi/8) c,t; 33 | } 34 | 35 | h q[0]; 36 | h q[1]; 37 | h q[2]; 38 | h q[3]; 39 | cu q[3],q[4]; 40 | cu q[2],q[4]; 41 | cu q[2],q[4]; 42 | cu q[1],q[4]; 43 | cu q[1],q[4]; 44 | cu q[1],q[4]; 45 | cu q[1],q[4]; 46 | cu q[0],q[4]; 47 | cu q[0],q[4]; 48 | cu q[0],q[4]; 49 | cu q[0],q[4]; 50 | cu q[0],q[4]; 51 | cu q[0],q[4]; 52 | cu q[0],q[4]; 53 | cu q[0],q[4]; 54 | h q[0]; 55 | cu1(-pi/2) q[0],q[1]; 56 | h q[1]; 57 | cu1(-pi/4) q[0],q[2]; 58 | cu1(-pi/2) q[1],q[2]; 59 | h q[2]; 60 | cu1(-pi/8) q[0],q[3]; 61 | cu1(-pi/4) q[1],q[3]; 62 | cu1(-pi/2) q[2],q[3]; 63 | h q[3]; 64 | measure q[0] -> c[0]; 65 | measure q[1] -> c[1]; 66 | measure q[2] -> c[2]; 67 | measure q[3] -> c[3]; 68 | """ 69 | } 70 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/Register.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | public protocol RegisterArgument { 19 | var identifier: String { get } 20 | } 21 | 22 | public protocol Register: RegisterArgument, CustomStringConvertible { 23 | var name:String { get } 24 | var size:Int { get } 25 | } 26 | 27 | extension Register { 28 | 29 | public var identifier: String { 30 | return self.name 31 | } 32 | 33 | /** 34 | Check that i is a valid index. 35 | */ 36 | public func check_range(_ i: Int) throws { 37 | if i < 0 || i >= self.size { 38 | throw QISKitError.regIndexRange 39 | } 40 | } 41 | 42 | func checkProperties() throws { 43 | if self.size <= 0 { 44 | throw QISKitError.regSize 45 | } 46 | let wholeRange = self.name.startIndex.. c; 64 | """ 65 | } 66 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeIf.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM if statement. 21 | children[0] is an id node. 22 | children[1] is an integer. 23 | children[2] is quantum operation node, including U, CX, custom_unitary, 24 | measure, reset, (and BUG: barrier, if). 25 | */ 26 | final class NodeIf: Node { 27 | 28 | let nodeId: Node 29 | let nodeNNInt: Node 30 | let nodeQop: Node 31 | 32 | init(identifier: Node, nninteger: Node, qop: Node) { 33 | nodeId = identifier 34 | nodeNNInt = nninteger 35 | nodeQop = qop 36 | } 37 | 38 | var type: NodeType { 39 | return .N_IF 40 | } 41 | 42 | var children: [Node] { 43 | return [self.nodeId,self.nodeNNInt,self.nodeQop] 44 | } 45 | 46 | func qasm(_ prec: Int) -> String { 47 | var qasm: String = "if" 48 | qasm += " (\(self.nodeId.qasm(prec))" 49 | qasm += " == \(self.nodeNNInt.qasm(prec))" 50 | qasm += " ) \(self.nodeQop.qasm(prec))" 51 | return qasm 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/charts/BarChartConfiguration.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #if os(OSX) || os(iOS) 17 | 18 | // MARK: - Main body 19 | 20 | struct BarChartConfiguration { 21 | 22 | // MARK: - Public properties 23 | 24 | let xAxisName: String 25 | let alignXAxisTickWithLabel: Bool 26 | let showXAxisSplitLine: Bool 27 | let yAxisName: String 28 | let showYAxisSplitLine: Bool 29 | let showValueOnTop: Bool 30 | 31 | // MARK: - Init methods 32 | 33 | init(xAxisName: String = "", 34 | alignXAxisTickWithLabel: Bool = false, 35 | showXAxisSplitLine: Bool = false, 36 | yAxisName: String = "", 37 | showYAxisSplitLine: Bool = false, 38 | showValueOnTop: Bool = false) { 39 | self.xAxisName = xAxisName 40 | self.alignXAxisTickWithLabel = alignXAxisTickWithLabel 41 | self.showXAxisSplitLine = showXAxisSplitLine 42 | self.yAxisName = yAxisName 43 | self.showYAxisSplitLine = showYAxisSplitLine 44 | self.showValueOnTop = showValueOnTop 45 | } 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/QasmError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | public enum QasmError: LocalizedError, CustomStringConvertible { 19 | 20 | case errorBinop(qasm: String) 21 | case errorPrefix(qasm: String) 22 | case errorExternal(qasm: String) 23 | case errorLocalParameter(qasm: String) 24 | case internalError(error: Error) 25 | 26 | public var errorDescription: String? { 27 | return self.description 28 | } 29 | 30 | public var description: String { 31 | switch self { 32 | case .errorBinop(let qasm): 33 | return "internal error: undefined binop: qasm='\(qasm)'" 34 | case .errorPrefix(let qasm): 35 | return "internal error: undefined prefix: qasm='\(qasm)'" 36 | case .errorExternal(let qasm): 37 | return "internal error: undefined external: qasm='\(qasm)'" 38 | case .errorLocalParameter(let qasm): 39 | return "expected local parameter name: qasm='\(qasm)'" 40 | case .internalError(let error): 41 | return error.localizedDescription 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeReal.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Node for an OPENQASM real number. 20 | This node has no children. The data is in the value field. 21 | */ 22 | class NodeReal: NodeRealValue { 23 | 24 | let value: Double 25 | 26 | init(id: Double) { 27 | self.value = id 28 | } 29 | 30 | var type: NodeType { 31 | return .N_REAL 32 | } 33 | 34 | var children: [Node] { 35 | return [] 36 | } 37 | 38 | func qasm(_ prec: Int) -> String { 39 | return self.value.format(prec) 40 | } 41 | 42 | func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue { 43 | return SymbolicValue(self.value) 44 | } 45 | } 46 | 47 | final class NodeRealPI: NodeReal { 48 | 49 | init() { 50 | super.init(id: Double.pi) 51 | } 52 | 53 | override func qasm(_ prec: Int) -> String { 54 | return SymbolicValue.pi.format(prec) 55 | } 56 | 57 | override func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue { 58 | return SymbolicValue.pi 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeGopList.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | final class NodeGopList: Node { 20 | 21 | private(set) var gateops: [Node] 22 | 23 | init(gateop: Node) { 24 | self.gateops = [gateop] 25 | } 26 | 27 | func addIdentifier(gateop: Node) { 28 | self.gateops.append(gateop) 29 | } 30 | 31 | func calls() -> [String] { 32 | // Return a list of custom gate names in this gate body.""" 33 | var _calls: [String] = [] 34 | for g in self.gateops { 35 | if let gop = g as? NodeCustomUnitary { 36 | _calls.append(gop.name) 37 | } 38 | } 39 | return _calls 40 | } 41 | 42 | var type: NodeType { 43 | return .N_GATEOPLIST 44 | } 45 | 46 | var children: [Node] { 47 | return self.gateops 48 | } 49 | 50 | func qasm(_ prec: Int) -> String { 51 | let qasms: [String] = self.gateops.compactMap({ (node: Node) -> String in 52 | return node.qasm(prec) 53 | }) 54 | return qasms.joined(separator: "\n") 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Sources/qiskit/backends/SimulatorError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Exception for errors raised by the Simulator object. 20 | */ 21 | public enum SimulatorError: LocalizedError, CustomStringConvertible { 22 | case unrecognizedOperation(backend: String, operation: String) 23 | case notImplemented(backend: String) 24 | case missingCompiledCircuit 25 | case missingOperationName 26 | case simulationCancelled 27 | 28 | public var errorDescription: String? { 29 | return self.description 30 | } 31 | public var description: String { 32 | switch self { 33 | case .unrecognizedOperation(let backend,let operation): 34 | return "\(backend) encountered unrecognized operation '\(operation)'" 35 | case .notImplemented(let backend): 36 | return "\(backend) not implemented" 37 | case .missingCompiledCircuit: 38 | return "Missing compiled circuit." 39 | case .missingOperationName: 40 | return "Missing Operation name" 41 | case .simulationCancelled: 42 | return "Simulation cancelled." 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/Node.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | enum NodeType: String { 19 | case N_BARRIER = "barrier" 20 | case N_BINARYOP = "binop" 21 | case N_CNOT = "cnot" 22 | case N_CREG = "creg" 23 | case N_CUSTOMUNITARY = "custom_unitary" 24 | case N_EXPRESSIONLIST = "expression_list" 25 | case N_EXTERNAL = "external" 26 | case N_GATE = "gate" 27 | case N_GATEBODY = "gate_body" 28 | case N_GATEOPLIST = "gateoplist" 29 | case N_ID = "id" 30 | case N_IDLIST = "id_list" 31 | case N_IF = "if" 32 | case N_INCLUDE = "incld" 33 | case N_INDEXEDID = "indexed_id" 34 | case N_INT = "int" 35 | case N_MAGIC = "magic" 36 | case N_MAINPROGRAM = "main_program" 37 | case N_MEASURE = "measure" 38 | case N_OPAQUE = "opaque" 39 | case N_PREFIX = "prefix" 40 | case N_PRIMARYLIST = "primary_list" 41 | case N_PROGRAM = "program" 42 | case N_REAL = "real" 43 | case N_RESET = "reset" 44 | case N_QREG = "qreg" 45 | case N_UNIVERSALUNITARY = "universal_unitary" 46 | } 47 | 48 | protocol Node { 49 | var type: NodeType { get } 50 | var children: [Node] { get } 51 | 52 | func qasm(_ prec: Int) -> String 53 | } 54 | -------------------------------------------------------------------------------- /Sources/qiskit/unroll/BackendError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Exception for errors raised by unroller backends. 20 | */ 21 | public enum BackendError: LocalizedError, CustomStringConvertible { 22 | case errorOpaque(name: String) 23 | case qregNotExist(name: String) 24 | case cregNotExist(name: String) 25 | case gateNotExist(name: String) 26 | case gateIncompatible(name: String,args: Int, qubits: Int) 27 | 28 | public var errorDescription: String? { 29 | return self.description 30 | } 31 | public var description: String { 32 | switch self { 33 | case .errorOpaque(let name): 34 | return "opaque gate \(name) not in basis" 35 | case .qregNotExist(let name): 36 | return "qreg \(name) does not exist" 37 | case .cregNotExist(let name): 38 | return "creg \(name) does not exist" 39 | case .gateNotExist(let name): 40 | return "gate \(name) not in standard extensions" 41 | case .gateIncompatible(let name,let args, let qubits): 42 | return "gate \(name) signature [\(args),\(qubits)] is incompatible with the standard extensions" 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/InstructionSet.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | public final class InstructionSet { 19 | 20 | private var instructions: [Instruction] = [] 21 | 22 | /** 23 | Add instruction to set. 24 | */ 25 | public func add(_ instruction: Instruction) { 26 | self.instructions.append(instruction) 27 | } 28 | 29 | /** 30 | Invert all instructions 31 | */ 32 | public func inverse() -> InstructionSet { 33 | for instruction in self.instructions { 34 | _ = instruction.inverse() 35 | } 36 | return self 37 | } 38 | 39 | /** 40 | Add controls to all instructions. 41 | */ 42 | public func q_if(_ qregs:[QuantumRegister]) -> InstructionSet { 43 | for instruction in self.instructions { 44 | instruction.q_if(qregs) 45 | } 46 | return self 47 | } 48 | 49 | /** 50 | Add classical control register to all instructions 51 | */ 52 | public func c_if(_ c: ClassicalRegister, _ val: Int) throws -> InstructionSet { 53 | for instruction in self.instructions { 54 | try instruction.c_if(c, val) 55 | } 56 | return self 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/RegBit.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | public typealias TupleRegBit = HashableTuple 19 | 20 | public struct RegBit: Hashable, CustomStringConvertible { 21 | 22 | private let tuple: HashableTuple 23 | 24 | public var name: String { 25 | return self.tuple.one 26 | } 27 | public var index: Int { 28 | return self.tuple.two 29 | } 30 | public var qasm: String { 31 | return RegBit.qasm(self.name,self.index) 32 | } 33 | public var description: String { 34 | return self.qasm 35 | } 36 | 37 | public init(_ name: String, _ index: Int) { 38 | self.tuple = HashableTuple(name,index) 39 | } 40 | 41 | public init(_ value: (String,Int)) { 42 | self.tuple = HashableTuple(value.0,value.1) 43 | } 44 | 45 | public static func qasm(_ name: String, _ index: Int) -> String { 46 | return "\(name)[\(index)]" 47 | } 48 | 49 | public var hashValue : Int { 50 | get { 51 | return self.tuple.hashValue 52 | } 53 | } 54 | public static func ==(lhs: RegBit, rhs:RegBit) -> Bool { 55 | return lhs.tuple == rhs.tuple 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeQreg.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Node for an OPENQASM qreg statement. 20 | children[0] is an indexedid node. 21 | */ 22 | final class NodeQreg: Node { 23 | 24 | let indexedid: Node 25 | private(set) var _name: String = "" 26 | private(set) var line: Int = 0 27 | private(set) var file: String = "" 28 | private(set) var index: Int = 0 29 | 30 | init(indexedid: Node, line: Int, file: String) { 31 | self.indexedid = indexedid 32 | if let _id = self.indexedid as? NodeIndexedId { 33 | // Name of the qreg 34 | self._name = _id.name 35 | // Source line number 36 | self.line = _id.line 37 | // Source file name 38 | self.file = _id.file 39 | // Size of the register 40 | self.index = _id.index 41 | } 42 | } 43 | 44 | var type: NodeType { 45 | return .N_QREG 46 | } 47 | 48 | var name: String { 49 | return _name 50 | } 51 | 52 | var children: [Node] { 53 | return [self.indexedid] 54 | } 55 | 56 | func qasm(_ prec: Int) -> String { 57 | return "qreg " + self.indexedid.qasm(prec) + ";" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/ipea_3_pi_8.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Name of Experiment: ipea_3*pi/8 v2 17 | struct Ipea3pi8 { 18 | static let QASM = """ 19 | OPENQASM 2.0; 20 | include "qelib1.inc"; 21 | 22 | qreg q[2]; 23 | creg c[4]; 24 | gate cu1fixed (a) c,t { 25 | u1 (-a) t; 26 | cx c,t; 27 | u1 (a) t; 28 | cx c,t; 29 | } 30 | gate cu c,t { 31 | cu1fixed (3*pi/8) c,t; 32 | } 33 | 34 | h q[0]; 35 | cu q[0],q[1]; 36 | cu q[0],q[1]; 37 | cu q[0],q[1]; 38 | cu q[0],q[1]; 39 | cu q[0],q[1]; 40 | cu q[0],q[1]; 41 | cu q[0],q[1]; 42 | cu q[0],q[1]; 43 | h q[0]; 44 | measure q[0] -> c[0]; 45 | reset q[0]; 46 | h q[0]; 47 | cu q[0],q[1]; 48 | cu q[0],q[1]; 49 | cu q[0],q[1]; 50 | cu q[0],q[1]; 51 | if(c==1) u1(-pi/2) q[0]; 52 | h q[0]; 53 | measure q[0] -> c[1]; 54 | reset q[0]; 55 | h q[0]; 56 | cu q[0],q[1]; 57 | cu q[0],q[1]; 58 | if(c==1) u1(-pi/4) q[0]; 59 | if(c==2) u1(-pi/2) q[0]; 60 | if(c==3) u1(-3*pi/4) q[0]; 61 | h q[0]; 62 | measure q[0] -> c[2]; 63 | reset q[0]; 64 | h q[0]; 65 | cu q[0],q[1]; 66 | if(c==1) u1(-pi/8) q[0]; 67 | if(c==2) u1(-2*pi/8) q[0]; 68 | if(c==3) u1(-3*pi/8) q[0]; 69 | if(c==4) u1(-4*pi/8) q[0]; 70 | if(c==5) u1(-5*pi/8) q[0]; 71 | if(c==6) u1(-6*pi/8) q[0]; 72 | if(c==7) u1(-7*pi/8) q[0]; 73 | h q[0]; 74 | measure q[0] -> c[3]; 75 | """ 76 | } 77 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/Reset.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Qubit reset 20 | */ 21 | public final class Reset: Instruction, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | init(_ qubit: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("reset", [], [qubit], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return Reset(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return "\(name) \(self.args[0].identifier)" 39 | } 40 | 41 | @discardableResult 42 | public func inverse() -> Reset { 43 | preconditionFailure("inverse not implemented") 44 | } 45 | 46 | /** 47 | Reapply this gate to corresponding qubits in circ. 48 | */ 49 | public func reapply(_ circ: QuantumCircuit) throws { 50 | try self._modifiers(try circ.reset(self.args[0] as! QuantumRegisterTuple)) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeCreg.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM creg statement. 21 | children[0] is an indexedid node. 22 | */ 23 | final class NodeCreg: Node { 24 | 25 | let indexedid: Node 26 | private(set) var _name: String = "" 27 | private(set) var line: Int = 0 28 | private(set) var file: String = "" 29 | private(set) var index: Int = 0 30 | 31 | init(indexedid: Node, line: Int, file: String) { 32 | self.indexedid = indexedid 33 | if let _id = self.indexedid as? NodeIndexedId { 34 | // Name of the qreg 35 | self._name = _id.name 36 | // Source line number 37 | self.line = _id.line 38 | // Source file name 39 | self.file = _id.file 40 | // Size of the register 41 | self.index = _id.index 42 | } 43 | } 44 | 45 | var type: NodeType { 46 | return .N_CREG 47 | } 48 | 49 | var name: String { 50 | return self._name 51 | } 52 | 53 | var children: [Node] { 54 | return [self.indexedid] 55 | } 56 | 57 | func qasm(_ prec: Int) -> String { 58 | return "creg " + self.indexedid.qasm(prec) + ";" 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Sources/qiskit/datastructures/graph/GraphVertex.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | final class GraphVertex : Hashable { 19 | 20 | public let key: Int 21 | public var data: VertexDataType? = nil 22 | public var neighbors: [Int] { 23 | get { 24 | return self._neighbors.sorted() 25 | } 26 | } 27 | public var hashValue : Int { 28 | get { 29 | return self.key.hashValue 30 | } 31 | } 32 | private var _neighbors: Set = [] 33 | 34 | public init(_ key: Int) { 35 | self.key = key 36 | } 37 | 38 | func addNeighbor(_ key: Int) { 39 | self._neighbors.update(with: key) 40 | } 41 | 42 | func removeNeighbor(_ key: Int) { 43 | self._neighbors.remove(key) 44 | } 45 | 46 | public func copy() -> GraphVertex { 47 | let copy = GraphVertex(self.key) 48 | if self.data != nil { 49 | let d = self.data!.copy() as! VertexDataType 50 | copy.data = d 51 | } 52 | copy._neighbors = self._neighbors 53 | return copy 54 | } 55 | 56 | public static func ==(lhs: GraphVertex, rhs: GraphVertex) -> Bool { 57 | return lhs.key == rhs.key 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeIndexedId.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM indexed id. 21 | children[0] is an id node. 22 | children[1] is an integer (not a node). 23 | */ 24 | final class NodeIndexedId: Node { 25 | 26 | let identifier: Node 27 | private(set) var _name: String = "" 28 | private(set) var line: Int = 0 29 | private(set) var file: String = "" 30 | private(set) var index: Int = -1 31 | 32 | init(identifier: Node, index: Node) { 33 | self.identifier = identifier 34 | if let _nnInt = index as? NodeNNInt { 35 | self.index = _nnInt.value 36 | } 37 | if let _id = self.identifier as? NodeId { 38 | // Name of the qreg 39 | self._name = _id.name 40 | // Source line number 41 | self.line = _id.line 42 | // Source file name 43 | self.file = _id.file 44 | } 45 | } 46 | 47 | var type: NodeType { 48 | return .N_INDEXEDID 49 | } 50 | 51 | var name: String { 52 | return self._name 53 | } 54 | 55 | var children: [Node] { 56 | return [] 57 | } 58 | 59 | func qasm(_ prec: Int) -> String { 60 | var qasm: String = "\(self.identifier.qasm(prec))" 61 | if self.index >= 0 { 62 | qasm += " [\(self.index)]" 63 | } 64 | return qasm 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodePrefix.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /* 19 | Node for an OPENQASM prefix expression. 20 | children[0] is a prefix string such as '-'. 21 | children[1] is an expression node. 22 | */ 23 | final class NodePrefix: NodeRealValue { 24 | 25 | let op: String 26 | let _children: [Node] 27 | 28 | init(op: String, children: [Node]) { 29 | self.op = op 30 | self._children = children 31 | } 32 | 33 | var type: NodeType { 34 | return .N_PREFIX 35 | } 36 | 37 | var children: [Node] { 38 | return self._children 39 | } 40 | 41 | func qasm(_ prec: Int) -> String { 42 | let operand = self._children[0] 43 | if operand.type == .N_BINARYOP { 44 | return "\(op) (\(operand.qasm(prec)))" 45 | } 46 | return "\(op)\(operand.qasm(prec))" 47 | } 48 | 49 | func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue { 50 | let operation = self.op 51 | guard let operand = self._children[0] as? NodeRealValue else { 52 | throw QasmError.errorPrefix(qasm: self.qasm(15)) 53 | } 54 | let expr = try operand.real(nested_scope) 55 | if operation == "+" { 56 | return expr 57 | } 58 | if operation == "-" { 59 | return -expr 60 | } 61 | throw QasmError.errorPrefix(qasm: self.qasm(15)) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/Measure.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Quantum measurement in the computational basis. 20 | */ 21 | public final class Measure: Instruction, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | init(_ qubit: QuantumRegisterTuple, _ bit: ClassicalRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("measure", [], [qubit,bit], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return Measure(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return "\(name) \(self.args[0].identifier) -> \(self.args[1].identifier)" 39 | } 40 | 41 | @discardableResult 42 | public func inverse() -> Measure { 43 | preconditionFailure("inverse not implemented") 44 | } 45 | 46 | /** 47 | Reapply this instruction to corresponding qubits in circ. 48 | */ 49 | public func reapply(_ circ: QuantumCircuit) throws { 50 | try self._modifiers(circ.measure(self.args[0] as! QuantumRegisterTuple, 51 | self.args[1] as! ClassicalRegisterTuple)) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/ibmqx2/qe_qft_4.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // quantum Fourier transform for the Quantum Experience 17 | // on the first 4 qubits 18 | struct QE_QFT_4 { 19 | static let QASM = """ 20 | OPENQASM 2.0; 21 | include "qelib1.inc"; 22 | 23 | // Register declarations 24 | qreg q[5]; 25 | creg c[5]; 26 | 27 | // Choose starting state 28 | // ** Put your code here ** 29 | 30 | // Quantum Fourier transform 31 | 32 | h q[0]; 33 | // cu1(pi/2) q[0],q[1]; 34 | u1(pi/4) q[0]; 35 | cx q[0],q[1]; 36 | u1(-pi/4) q[1]; 37 | cx q[0],q[1]; 38 | u1(pi/4) q[1]; 39 | // end cu1 40 | h q[1]; 41 | // cu1(pi/4) q[0],q[2]; 42 | u1(pi/8) q[0]; 43 | cx q[0],q[2]; 44 | u1(-pi/8) q[2]; 45 | cx q[0],q[2]; 46 | u1(pi/8) q[2]; 47 | // end cu1 48 | // cu1(pi/2) q[1],q[2]; 49 | u1(pi/4) q[1]; 50 | cx q[1],q[2]; 51 | u1(-pi/4) q[2]; 52 | cx q[1],q[2]; 53 | u1(pi/4) q[2]; 54 | // end cu1 55 | h q[2]; 56 | // cu1swp(pi/2) q[3],q[2]; 57 | cx q[3],q[2]; 58 | h q[3]; h q[2]; 59 | cx q[3],q[2]; 60 | h q[3]; h q[2]; 61 | u1(-pi/4) q[2]; 62 | cx q[3],q[2]; 63 | u1(pi/4) q[3]; 64 | u1(pi/4) q[2]; 65 | // end cu1swp 66 | // cu1(pi/8) q[0],q[2]; 67 | u1(pi/16) q[0]; 68 | cx q[0],q[2]; 69 | u1(-pi/16) q[2]; 70 | cx q[0],q[2]; 71 | u1(pi/16) q[2]; 72 | // end cu1 73 | // cu1(pi/4) q[1],q[2]; 74 | u1(pi/8) q[1]; 75 | cx q[1],q[2]; 76 | u1(-pi/8) q[2]; 77 | cx q[1],q[2]; 78 | u1(pi/8) q[2]; 79 | // end cu1 80 | h q[2]; 81 | 82 | // Outputs are q[2], q[3], q[1], q[0] 83 | 84 | // Choose measurement 85 | // ** Put your code here ** 86 | 87 | measure q -> c; 88 | """ 89 | } 90 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeGateBody.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM custom gate body. 21 | children is a list of gate operation nodes. 22 | These are one of barrier, custom_unitary, U, or CX. 23 | */ 24 | final class NodeGateBody: Node { 25 | 26 | let goplist: Node? 27 | 28 | init() { 29 | self.goplist = nil 30 | } 31 | 32 | init(goplist: Node) { 33 | self.goplist = goplist 34 | } 35 | 36 | func calls() -> [String] { 37 | // Return a list of custom gate names in this gate body.""" 38 | var _calls: [String] = [] 39 | if let glist = goplist as? NodeGopList { 40 | for g in glist.gateops { 41 | if let gop = g as? NodeCustomUnitary { 42 | _calls.append(gop.name) 43 | } 44 | } 45 | } 46 | return _calls 47 | } 48 | 49 | var type: NodeType { 50 | return .N_GATEBODY 51 | } 52 | 53 | var children: [Node] { 54 | if let glist = goplist as? NodeGopList { 55 | return glist.children 56 | } 57 | return [] 58 | } 59 | 60 | func qasm(_ prec: Int) -> String { 61 | var qasms: [String] = [] 62 | if let glist = goplist as? NodeGopList { 63 | qasms = glist.children.compactMap({ (node: Node) -> String in 64 | return node.qasm(prec) 65 | }) 66 | } 67 | return qasms.joined(separator: "\n") 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Sources/qiskit/backends/QasmCppSimulator.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | #if os(Linux) 18 | import Dispatch 19 | #endif 20 | 21 | final class QasmCppSimulator: BaseBackend { 22 | 23 | public required init(_ configuration: [String:Any]? = nil) { 24 | super.init(configuration) 25 | if let conf = configuration { 26 | self._configuration = conf 27 | } 28 | else { 29 | self._configuration = ["name": "local_qasm_cpp_simulator", 30 | "url": "https://github.com/IBM/qiskit-sdk-swift", 31 | "exe": "qasm_simulator", 32 | "simulator": true, 33 | "local": true, 34 | "description": "A c++ simulator for qasm files", 35 | "coupling_map": "all-to-all", 36 | "basis_gates": "u1,u2,u3,cx,id" 37 | ] 38 | } 39 | } 40 | 41 | /** 42 | Run simulation on C++ simulator. 43 | */ 44 | @discardableResult 45 | override public func run(_ q_job: QuantumJob, response: @escaping ((_:Result) -> Void)) -> RequestTask { 46 | let reqTask = RequestTask() 47 | DispatchQueue.global(qos: .background).async { 48 | DispatchQueue.main.async { 49 | let job_id = UUID().uuidString 50 | response(Result(["job_id": job_id, "status": "ERROR","result": SimulatorError.notImplemented(backend: self.configuration["name"] as! String)],q_job.qobj)) 51 | } 52 | } 53 | return reqTask 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/generic/bigadder.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // quantum ripple-carry adder 17 | // 8-bit adder made out of 2 4-bit adders from adder.qasm 18 | // Cuccaro et al, quant-ph/0410184 19 | struct BigAdder { 20 | static let QASM = """ 21 | OPENQASM 2.0; 22 | include "qelib1.inc"; 23 | gate majority a,b,c 24 | { 25 | cx c,b; 26 | cx c,a; 27 | ccx a,b,c; 28 | } 29 | gate unmaj a,b,c 30 | { 31 | ccx a,b,c; 32 | cx c,a; 33 | cx a,b; 34 | } 35 | 36 | // add a to b, storing result in b 37 | gate add4 a0,a1,a2,a3,b0,b1,b2,b3,cin,cout 38 | { 39 | majority cin,b0,a0; 40 | majority a0,b1,a1; 41 | majority a1,b2,a2; 42 | majority a2,b3,a3; 43 | cx a3,cout; 44 | unmaj a2,b3,a3; 45 | unmaj a1,b2,a2; 46 | unmaj a0,b1,a1; 47 | unmaj cin,b0,a0; 48 | } 49 | 50 | // add two 8-bit numbers by calling the 4-bit ripple-carry adder 51 | // carry bit on output lives in carry[0] 52 | qreg carry[2]; 53 | qreg a[8]; 54 | qreg b[8]; 55 | creg ans[8]; 56 | creg carryout[1]; 57 | // set input states 58 | x a[0]; // a = 00000001 59 | x b; 60 | x b[6]; // b = 10111111 61 | // output should be 11000000 0 62 | 63 | add4 a[0],a[1],a[2],a[3],b[0],b[1],b[2],b[3],carry[0],carry[1]; 64 | add4 a[4],a[5],a[6],a[7],b[4],b[5],b[6],b[7],carry[1],carry[0]; 65 | 66 | measure b[0] -> ans[0]; 67 | measure b[1] -> ans[1]; 68 | measure b[2] -> ans[2]; 69 | measure b[3] -> ans[3]; 70 | measure b[4] -> ans[4]; 71 | measure b[5] -> ans[5]; 72 | measure b[6] -> ans[6]; 73 | measure b[7] -> ans[7]; 74 | measure carry[0] -> carryout[0]; 75 | """ 76 | } 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | 4 | **We appreciate all kinds of help, so thank you!** 5 | 6 | You can contribute in many ways to this project. 7 | 8 | Issue reporting 9 | --------------- 10 | 11 | This is a good point to start, when you find a problem please add 12 | it to the `issue traker `_. 13 | The ideal report should include the steps to reproduce it. 14 | 15 | Doubts solving 16 | -------------- 17 | 18 | To help less advanced users is another wonderful way to start. You can 19 | help us to close some opened issues. This kind of tickets should be 20 | labeled as ``question``. 21 | 22 | Improvement proposal 23 | -------------------- 24 | 25 | If you have an idea for a new feature please open a ticket labeled as 26 | ``enhancement``. If you could also add a piece of code with the idea 27 | or a partial implementation it would be awesome. 28 | 29 | Code 30 | ---- 31 | 32 | This section include some tips that will help you to push source code. 33 | 34 | Dependencies 35 | ~~~~~~~~~~~~ 36 | 37 | Needed libraries should be installed using Carthage 38 | 39 | 40 | Test 41 | ~~~~ 42 | 43 | New features often imply changes in the existent tests or new ones are 44 | needed. Once they're updated/added run this be sure they keep passing: 45 | 46 | 47 | Style guide 48 | ~~~~~~~~~~~ 49 | 50 | Submit clean code and please make effort to follow existing conventions 51 | in order to keep it as readable as possible. 52 | 53 | Doc 54 | ~~~ 55 | 56 | Review the parts of the documentation regarding the new changes and 57 | update it if it's needed. 58 | 59 | Pull requests 60 | ~~~~~~~~~~~~~ 61 | 62 | We use `GitHub pull requests 63 | `_ to accept the 64 | contributions. You can explain yourself as much as you want. Please 65 | follow the next rules for the commit messages: 66 | 67 | - It should be formed by a one-line subject, followed by one line of 68 | white space. Followed by one or more descriptive paragraphs, each 69 | separated by one line of white space. All of them finished by a dot. 70 | - If it fixes an issue, it should include a reference to the issue ID 71 | in the first line of the commit. 72 | - It should provide enough information for a reviewer to understand the 73 | changes and their relation to the rest of the code. 74 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/QuantumRegister.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Qubits Register 20 | */ 21 | public struct QuantumRegister: Register, Equatable { 22 | 23 | public private(set) var name:String = "" 24 | public private(set) var size:Int = 0 25 | 26 | public init(_ name: String, _ size: Int) throws { 27 | self.name = name 28 | self.size = size 29 | try self.checkProperties() 30 | } 31 | 32 | private init() { 33 | } 34 | 35 | public subscript(index: Int) -> QuantumRegisterTuple { 36 | get { 37 | if index < 0 || index >= self.size { 38 | fatalError("Index out of range") 39 | } 40 | return QuantumRegisterTuple(self, index) 41 | } 42 | } 43 | 44 | public var description: String { 45 | return "qreg \(self.name)[\(self.size)]" 46 | } 47 | 48 | public static func ==(lhs: QuantumRegister, rhs: QuantumRegister) -> Bool { 49 | return lhs.name == rhs.name && lhs.size == rhs.size 50 | } 51 | } 52 | 53 | public struct QuantumRegisterTuple: RegisterArgument, Equatable { 54 | public let register: QuantumRegister 55 | public let index: Int 56 | 57 | init(_ register: QuantumRegister, _ index: Int) { 58 | self.register = register 59 | self.index = index 60 | } 61 | 62 | public var identifier: String { 63 | return "\(self.register.name)[\(self.index)]" 64 | } 65 | 66 | public static func ==(lhs: QuantumRegisterTuple, rhs: QuantumRegisterTuple) -> Bool { 67 | return lhs.register == rhs.register && lhs.index == rhs.index 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/ClassicalRegister.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Bits Register 20 | */ 21 | public struct ClassicalRegister: Register, Equatable { 22 | 23 | public private(set) var name:String = "" 24 | public private(set) var size:Int = 0 25 | 26 | public init(_ name: String, _ size: Int) throws { 27 | self.name = name 28 | self.size = size 29 | try self.checkProperties() 30 | } 31 | 32 | private init() { 33 | } 34 | 35 | public subscript(index: Int) -> ClassicalRegisterTuple { 36 | get { 37 | if index < 0 || index >= self.size { 38 | fatalError("Index out of range") 39 | } 40 | return ClassicalRegisterTuple(self, index) 41 | } 42 | } 43 | 44 | public var description: String { 45 | return "creg \(self.name)[\(self.size)]" 46 | } 47 | 48 | public static func ==(lhs: ClassicalRegister, rhs: ClassicalRegister) -> Bool { 49 | return lhs.name == rhs.name && lhs.size == rhs.size 50 | } 51 | } 52 | 53 | public struct ClassicalRegisterTuple: RegisterArgument, Equatable { 54 | public let register: ClassicalRegister 55 | public let index: Int 56 | 57 | init(_ register: ClassicalRegister, _ index: Int) { 58 | self.register = register 59 | self.index = index 60 | } 61 | 62 | public var identifier: String { 63 | return "\(self.register.name)[\(self.index)]" 64 | } 65 | 66 | public static func ==(lhs: ClassicalRegisterTuple, rhs: ClassicalRegisterTuple) -> Bool { 67 | return lhs.register == rhs.register && lhs.index == rhs.index 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/InstructionComponent.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Quantum computer instruction component. 20 | */ 21 | public final class InstructionComponent { 22 | 23 | let name: String 24 | var params: [Double] 25 | let args: [RegisterArgument] 26 | unowned var circuit: QuantumCircuit 27 | private var control: (ClassicalRegister, Int)? = nil 28 | 29 | init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.name = name 31 | self.params = params 32 | self.args = args 33 | self.circuit = circuit 34 | } 35 | 36 | /** 37 | Add classical control on register classical and value val. 38 | */ 39 | func c_if(_ classical: ClassicalRegister, _ val: Int) throws { 40 | if val < 0 { 41 | throw QISKitError.controlValueNegative 42 | } 43 | self.control = (classical, val) 44 | } 45 | 46 | /** 47 | Apply any modifiers of this instruction to another one. 48 | */ 49 | func _modifiers(_ instruction: Instruction) throws { 50 | if self.control != nil { 51 | if !self.circuit.has_register(self.control!.0) { 52 | throw QISKitError.controlRegNotFound(name: self.control!.0.name) 53 | } 54 | try instruction.c_if(self.control!.0, self.control!.1) 55 | } 56 | } 57 | 58 | /** 59 | Print an if statement if needed. 60 | */ 61 | func _qasmif(_ string: String) -> String { 62 | if self.control == nil { 63 | return string 64 | } 65 | return "if(\(self.control!.0.name)==\(self.control!.1)) \(string)" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeId.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM id. 21 | The node has no children but has fields name, line, and file. 22 | There is a flag is_bit that is set when XXXXX to help with scoping. 23 | */ 24 | final class NodeId: NodeRealValue { 25 | 26 | private(set) var _name: String = "" 27 | private(set) var line: Int = 0 28 | private(set) var file: String = "" 29 | private(set) var index: Int = 0 // FIXME where does the index come from? 30 | private(set) var is_bit: Bool = false 31 | 32 | init(identifier: String, line: Int) { 33 | self._name = identifier 34 | self.line = line 35 | self.file = "" // FIXME find the name 36 | self.is_bit = false 37 | } 38 | 39 | var type: NodeType { 40 | return .N_ID 41 | } 42 | 43 | var name: String { 44 | return self._name 45 | } 46 | 47 | var children: [Node] { 48 | return [] 49 | } 50 | 51 | func qasm(_ prec: Int) -> String { 52 | let qasm: String = _name 53 | return qasm 54 | } 55 | 56 | func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue { 57 | guard let scope = nested_scope else { 58 | throw QasmError.errorLocalParameter(qasm: self.qasm(15)) 59 | } 60 | guard let last = scope.last else { 61 | throw QasmError.errorLocalParameter(qasm: self.qasm(15)) 62 | } 63 | guard let arg = last[self.name] else { 64 | throw QasmError.errorLocalParameter(qasm: self.qasm(15)) 65 | } 66 | let endIndex: Int = scope.count - 1 67 | return try arg.real(Array(scope[0.. String { 70 | var qasm = "\(self.name)" 71 | if let args = self.arguments { 72 | qasm += " (" + args.qasm(prec) + ")" 73 | } 74 | qasm += " \(self.bitlist.qasm(prec));" 75 | return qasm 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Sources/qiskit/sdk/Instruction.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Generic quantum computer instruction. 20 | */ 21 | public protocol Instruction: CustomStringConvertible { 22 | 23 | var instructionComponent: InstructionComponent { get } 24 | @discardableResult 25 | func inverse() -> Self 26 | func reapply(_ circ: QuantumCircuit) throws 27 | } 28 | 29 | protocol CopyableInstruction { 30 | func copy(_ c: QuantumCircuit) -> Instruction 31 | } 32 | 33 | extension Instruction { 34 | 35 | public var name: String { 36 | return instructionComponent.name 37 | } 38 | 39 | public var params: [Double] { 40 | return instructionComponent.params 41 | } 42 | 43 | public var args: [RegisterArgument] { 44 | return instructionComponent.args 45 | } 46 | 47 | var circuit: QuantumCircuit { 48 | return instructionComponent.circuit 49 | } 50 | 51 | public var qasm: String { 52 | return self.description 53 | } 54 | 55 | /** 56 | Apply any modifiers of this instruction to another one. 57 | */ 58 | func _modifiers(_ instruction: Instruction) throws { 59 | try self.instructionComponent._modifiers(instruction) 60 | } 61 | 62 | /** 63 | Print an if statement if needed. 64 | */ 65 | func _qasmif(_ string: String) -> String { 66 | return self.instructionComponent._qasmif(string) 67 | } 68 | 69 | @discardableResult 70 | public func c_if(_ classical: ClassicalRegister, _ val: Int) throws -> Instruction { 71 | try self.instructionComponent.c_if(classical,val) 72 | return self 73 | } 74 | 75 | @discardableResult 76 | public func q_if(_ qregs:[QuantumRegister]) -> Instruction { 77 | preconditionFailure("q_if not implemented") 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeExternal.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM external function. 21 | children[0] is an id node with the name of the function. 22 | children[1] is an expression node. 23 | */ 24 | final class NodeExternal: NodeRealValue { 25 | 26 | private static let externalFunctions = ["sin", "cos", "tan", "exp", "ln", "sqrt"] 27 | 28 | let operation: String 29 | let expression: Node 30 | 31 | init(operation: String, expression: Node) { 32 | self.operation = operation 33 | self.expression = expression 34 | } 35 | 36 | var type: NodeType { 37 | return .N_EXTERNAL 38 | } 39 | 40 | var children: [Node] { 41 | return [self.expression] 42 | } 43 | 44 | func qasm(_ prec: Int) -> String { 45 | var qasm = self.operation 46 | qasm += "( \(self.expression.qasm(prec)) )" 47 | return qasm 48 | } 49 | 50 | func real(_ nested_scope: [[String:NodeRealValue]]?) throws -> SymbolicValue { 51 | if let expr = self.expression as? NodeRealValue { 52 | let arg = try expr.real(nested_scope) 53 | if self.operation == "sin" { 54 | return sin(arg) 55 | } 56 | if self.operation == "cos" { 57 | return cos(arg) 58 | } 59 | if self.operation == "tan" { 60 | return tan(arg) 61 | } 62 | if self.operation == "exp" { 63 | return exp(arg) 64 | } 65 | if self.operation == "ln" { 66 | return log(arg) 67 | } 68 | if self.operation == "sqrt" { 69 | return arg.squareRoot() 70 | } 71 | } 72 | throw QasmError.errorExternal(qasm: self.qasm(15)) 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Sources/qiskit/network/RequestTask.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Single or group os Asynchronous tasks 20 | */ 21 | public final class RequestTask { 22 | 23 | private let task: URLSessionDataTask? 24 | private var children: [RequestTask] = [] 25 | private var cancelled = false 26 | private let lock = NSRecursiveLock() 27 | 28 | public init() { 29 | self.task = nil 30 | } 31 | 32 | init(_ task: URLSessionDataTask) { 33 | self.task = task 34 | } 35 | 36 | /** 37 | Adds a task as child 38 | */ 39 | public func add(_ requestTask: RequestTask) { 40 | self.lock.lock() 41 | self.children.append(requestTask) 42 | if self.cancelled { 43 | requestTask.cancel() 44 | } 45 | self.lock.unlock() 46 | } 47 | 48 | /** 49 | Checkes is is a task has been cancelled 50 | */ 51 | public func isCancelled() -> Bool { 52 | self.lock.lock() 53 | let ret = self.cancelled 54 | self.lock.unlock() 55 | return ret 56 | } 57 | /** 58 | Cancels a task and all its children 59 | */ 60 | public func cancel() { 61 | self.lock.lock() 62 | if !self.cancelled { 63 | if let t = self.task { 64 | t.cancel() 65 | } 66 | for requestTask in self.children { 67 | requestTask.cancel() 68 | } 69 | self.cancelled = true 70 | } 71 | self.lock.unlock() 72 | } 73 | 74 | public static func + (left: RequestTask, right: RequestTask) -> RequestTask { 75 | let ret = RequestTask() 76 | ret.add(left) 77 | ret.add(right) 78 | return ret 79 | } 80 | 81 | public static func += (left: inout RequestTask, right: RequestTask) { 82 | left.add(right) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /qiskit.xcodeproj/xcshareddata/xcschemes/qiskitTests.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 16 | 18 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 41 | 42 | 46 | 47 | 51 | 52 | 53 | 54 | 55 | 56 | 62 | 63 | 65 | 66 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/ToolsError.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Tools Exceptions 20 | */ 21 | public enum ToolsError: LocalizedError, CustomStringConvertible { 22 | 23 | case unknownHamiltonian 24 | case invalidPauliMultiplication 25 | case pauliToMatrixZ 26 | case pauliToMatrixX 27 | case invalidPauliString(label: String) 28 | case errorPauliGroup 29 | case errorPartialTrace 30 | case errorSubsystem 31 | case errorVectorize 32 | case invalidMethod(method: String) 33 | case errorVectorizedMatrix 34 | case errorConcurrence 35 | 36 | public var errorDescription: String? { 37 | return self.description 38 | } 39 | public var description: String { 40 | switch self { 41 | case .unknownHamiltonian: 42 | return "Unknown hamiltonian." 43 | case .invalidPauliMultiplication: 44 | return "Paulis cannot be multiplied - different number of qubits" 45 | case .pauliToMatrixZ: 46 | return "The z string is not of the form 0 and 1" 47 | case .pauliToMatrixX: 48 | return "The x string is not of the form 0 and 1" 49 | case .invalidPauliString(let label): 50 | return "Invalid Pauli string: '\(label)'" 51 | case .errorPauliGroup: 52 | return "Please set the number of qubits to less than 5" 53 | case .errorPartialTrace: 54 | return "Input is not a multi-qubit state, specifify input state dims" 55 | case .errorSubsystem: 56 | return "Invalid subsystem" 57 | case .errorVectorize: 58 | return "Input state must be n-qubit state" 59 | case .invalidMethod(let method): 60 | return "Invalid method: '\(method)'" 61 | case .errorVectorizedMatrix: 62 | return "Input is not a vectorized square matrix" 63 | case .errorConcurrence: 64 | return "Concurrence is not defined for more than two qubits" 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Sources/qiskit/tools/visualization/charts/StateQsphereChartValue.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2018 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #if os(OSX) || os(iOS) 17 | 18 | // MARK: - Main body 19 | 20 | struct StateQsphereChartValue { 21 | 22 | // MARK: - Types 23 | 24 | enum Color: Int { 25 | case blue = 0 26 | case blueViolet = 1 27 | case violet = 2 28 | case redViolet = 3 29 | case red = 4 30 | case redOrange = 5 31 | case orange = 6 32 | case orangeYellow = 7 33 | case yellow = 8 34 | case yellowGreen = 9 35 | case green = 10 36 | case greenBlue = 11 37 | 38 | var rgb : [Double] { 39 | switch self { 40 | case .blue: 41 | return [0.0, 0.0, 1.0] 42 | case .blueViolet: 43 | return [0.5, 0.0, 1.0] 44 | case .violet: 45 | return [1.0, 0.0, 1.0] 46 | case .redViolet: 47 | return [1.0, 0.0, 0.5] 48 | case .red: 49 | return [1.0, 0.0, 0.0] 50 | case .redOrange: 51 | return [1.0, 0.5, 0.0] 52 | case .orange: 53 | return [1.0, 1.0, 0.0] 54 | case .orangeYellow: 55 | return [0.5, 1.0, 0.0] 56 | case .yellow: 57 | return [0.0, 1.0, 0.0] 58 | case .yellowGreen: 59 | return [0.0, 1.0, 0.5] 60 | case .green: 61 | return [0.0, 1.0, 1.0] 62 | case .greenBlue: 63 | return [0.0, 0.5, 1.0] 64 | } 65 | } 66 | } 67 | 68 | // MARK: - Public properties 69 | 70 | let xValue: Double 71 | let yValue: Double 72 | let zValue: Double 73 | let alpha: Double 74 | let color: Color 75 | 76 | var xyz: [Double] { 77 | return [xValue, yValue, zValue] 78 | } 79 | 80 | var rgba: [Double] { 81 | var rgba = color.rgb 82 | rgba.append(alpha) 83 | 84 | return rgba 85 | } 86 | } 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /Sources/qiskit/qasm/node/NodeBinaryOp.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /* 20 | Node for an OPENQASM binary operation exprssion. 21 | children[0] is the operation, as a character. 22 | children[1] is the left expression. 23 | children[2] is the right expression. 24 | */ 25 | final class NodeBinaryOp: NodeRealValue { 26 | 27 | let op: String 28 | let _children: [Node] 29 | 30 | init(op: String, children: [Node]) { 31 | self.op = op 32 | self._children = children 33 | } 34 | 35 | var type: NodeType { 36 | return .N_BINARYOP 37 | } 38 | 39 | var children: [Node] { 40 | return self._children 41 | } 42 | 43 | func qasm(_ prec: Int) -> String { 44 | let lhs = _children[0] 45 | let rhs = _children[1] 46 | 47 | var lhsqasm = lhs.qasm(prec) 48 | if lhs.type == .N_BINARYOP { 49 | if (lhs as! NodeBinaryOp).op == "+" || (lhs as! NodeBinaryOp).op == "-" { 50 | lhsqasm = "(\(lhs.qasm(prec)))" 51 | } 52 | } 53 | 54 | return "\(lhsqasm) \(op) \(rhs.qasm(prec))" 55 | } 56 | 57 | func real(_ nested_scope: [[String:NodeRealValue]]? = nil) throws -> SymbolicValue { 58 | let operation = self.op 59 | guard let lexpr = self._children[0] as? NodeRealValue else { 60 | throw QasmError.errorBinop(qasm: self.qasm(15)) 61 | } 62 | guard let rexpr = self._children[1] as? NodeRealValue else { 63 | throw QasmError.errorBinop(qasm: self.qasm(15)) 64 | } 65 | let lhs = try lexpr.real(nested_scope) 66 | let rhs = try rexpr.real(nested_scope) 67 | if operation == "+" { 68 | return lhs + rhs 69 | } 70 | if operation == "-" { 71 | return lhs - rhs 72 | } 73 | if operation == "*" { 74 | return lhs * rhs 75 | } 76 | if operation == "/" { 77 | return lhs / rhs 78 | } 79 | if operation == "^" { 80 | return pow(lhs,rhs) 81 | } 82 | throw QasmError.errorBinop(qasm: self.qasm(15)) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Sources/cqasmparser/ParseTree.c: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | #include "ParseTree.h" 17 | 18 | void (*ParseSuccess)(NodeIdType); 19 | void (*ParseFail)(int,const char*); 20 | long (*GetIncludeContents)(const char*,char*,long); 21 | StringIdType (*AddString)(const char*); 22 | NodeIdType (*CreateBarrier)(NodeIdType); 23 | NodeIdType (*CreateBinaryOperation)(const char*,NodeIdType,NodeIdType); 24 | NodeIdType (*CreateCX)(NodeIdType,NodeIdType); 25 | NodeIdType (*CreateCReg)(NodeIdType); 26 | NodeIdType (*CreateCustomUnitary2)(NodeIdType,NodeIdType); 27 | NodeIdType (*CreateCustomUnitary3)(NodeIdType,NodeIdType,NodeIdType); 28 | NodeIdType (*CreateExpressionList1)(NodeIdType); 29 | NodeIdType (*CreateExpressionList2)(NodeIdType,NodeIdType); 30 | NodeIdType (*CreateExternal)(NodeIdType,StringIdType); 31 | NodeIdType (*CreateGate3)(NodeIdType,NodeIdType,NodeIdType); 32 | NodeIdType (*CreateGate4)(NodeIdType,NodeIdType,NodeIdType,NodeIdType); 33 | NodeIdType (*CreateGateBody0)(void); 34 | NodeIdType (*CreateGateBody1)(NodeIdType); 35 | NodeIdType (*CreateGopList1)(NodeIdType); 36 | NodeIdType (*CreateGopList2)(NodeIdType,NodeIdType); 37 | NodeIdType (*CreateId)(StringIdType,long); 38 | NodeIdType (*CreateIdlist1)(NodeIdType); 39 | NodeIdType (*CreateIdlist2)(NodeIdType,NodeIdType); 40 | NodeIdType (*CreateIf)(NodeIdType,NodeIdType,NodeIdType); 41 | NodeIdType (*CreateInclude)(StringIdType); 42 | NodeIdType (*CreateIndexedId)(NodeIdType,NodeIdType); 43 | NodeIdType (*CreateInt)(long); 44 | NodeIdType (*CreateMagic)(NodeIdType); 45 | NodeIdType (*CreateMainProgram2)(NodeIdType,NodeIdType); 46 | NodeIdType (*CreateMainProgram3)(NodeIdType,NodeIdType,NodeIdType); 47 | NodeIdType (*CreateMeasure)(NodeIdType,NodeIdType); 48 | NodeIdType (*CreateOpaque2)(NodeIdType,NodeIdType); 49 | NodeIdType (*CreateOpaque3)(NodeIdType,NodeIdType,NodeIdType); 50 | NodeIdType (*CreatePrefixOperation)(const char*,NodeIdType); 51 | NodeIdType (*CreatePrimaryList1)(NodeIdType); 52 | NodeIdType (*CreatePrimaryList2)(NodeIdType,NodeIdType); 53 | NodeIdType (*CreateProgram1)(NodeIdType); 54 | NodeIdType (*CreateProgram2)(NodeIdType,NodeIdType); 55 | NodeIdType (*CreateQReg)(NodeIdType); 56 | NodeIdType (*CreateReal)(double); 57 | NodeIdType (*CreateRealPI)(void); 58 | NodeIdType (*CreateReset)(NodeIdType); 59 | NodeIdType (*CreateUniversalUnitary)(NodeIdType,NodeIdType); 60 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CY.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /** 20 | controlled-Y gate. 21 | */ 22 | public final class CyGate: Gate, CopyableInstruction { 23 | 24 | public let instructionComponent: InstructionComponent 25 | 26 | fileprivate init(_ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 27 | self.instructionComponent = InstructionComponent("cy", [], [ctl,tgt], circuit) 28 | } 29 | 30 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 31 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 32 | } 33 | 34 | func copy(_ c: QuantumCircuit) -> Instruction { 35 | return CyGate(self.name, self.params, self.args, c) 36 | } 37 | 38 | public var description: String { 39 | return self._qasmif("\(name) \(self.args[0].identifier),\(self.args[1].identifier)") 40 | } 41 | 42 | /** 43 | Invert this gate. 44 | */ 45 | @discardableResult 46 | public func inverse() -> CyGate { 47 | return self 48 | } 49 | 50 | /** 51 | Reapply this gate to corresponding qubits in circ. 52 | */ 53 | public func reapply(_ circ: QuantumCircuit) throws { 54 | try self._modifiers(circ.cy(self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 55 | } 56 | } 57 | 58 | extension QuantumCircuit { 59 | 60 | /** 61 | Apply CY to circuit. 62 | */ 63 | @discardableResult 64 | public func cy(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CyGate { 65 | try self._check_qubit(ctl) 66 | try self._check_qubit(tgt) 67 | try QuantumCircuit._check_dups([ctl, tgt]) 68 | return self._attach(CyGate(ctl, tgt, self)) as! CyGate 69 | } 70 | } 71 | 72 | extension CompositeGate { 73 | 74 | /** 75 | Apply CY to circuit. 76 | */ 77 | @discardableResult 78 | public func cy(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CyGate { 79 | try self._check_qubit(ctl) 80 | try self._check_qubit(tgt) 81 | try QuantumCircuit._check_dups([ctl, tgt]) 82 | return self._attach(CyGate(ctl, tgt, self.circuit)) as! CyGate 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CH.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | controlled-H gate. 20 | */ 21 | public final class CHGate: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("ch", [], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return CHGate(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return self._qasmif("\(name) \(self.args[0].identifier),\(self.args[1].identifier)") 39 | } 40 | 41 | /** 42 | Invert this gate. 43 | */ 44 | @discardableResult 45 | public func inverse() -> CHGate { 46 | return self 47 | } 48 | 49 | /** 50 | Reapply this gate to corresponding qubits in circ. 51 | */ 52 | public func reapply(_ circ: QuantumCircuit) throws { 53 | try self._modifiers(circ.ch(self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 54 | } 55 | } 56 | 57 | extension QuantumCircuit { 58 | 59 | /** 60 | Apply CH from ctl to tgt. 61 | */ 62 | @discardableResult 63 | public func ch(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CHGate { 64 | try self._check_qubit(ctl) 65 | try self._check_qubit(tgt) 66 | try QuantumCircuit._check_dups([ctl, tgt]) 67 | return self._attach(CHGate(ctl, tgt, self)) as! CHGate 68 | } 69 | } 70 | 71 | extension CompositeGate { 72 | 73 | /** 74 | Apply CH from ctl to tgt. 75 | */ 76 | @discardableResult 77 | public func ch(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CHGate { 78 | try self._check_qubit(ctl) 79 | try self._check_qubit(tgt) 80 | try QuantumCircuit._check_dups([ctl, tgt]) 81 | return self._attach(CHGate(ctl, tgt, self.circuit)) as! CHGate 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CZ.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | controlled-Phase gate. 20 | */ 21 | public final class CzGate: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("cz", [], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return CzGate(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return self._qasmif("\(name) \(self.args[0].identifier),\(self.args[1].identifier)") 39 | } 40 | 41 | /** 42 | Invert this gate. 43 | */ 44 | @discardableResult 45 | public func inverse() -> CzGate { 46 | return self 47 | } 48 | 49 | /** 50 | Reapply this gate to corresponding qubits in circ. 51 | */ 52 | public func reapply(_ circ: QuantumCircuit) throws { 53 | try self._modifiers(circ.cz(self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 54 | } 55 | } 56 | 57 | extension QuantumCircuit { 58 | 59 | /** 60 | Apply CZ to circuit. 61 | */ 62 | @discardableResult 63 | public func cz(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CzGate { 64 | try self._check_qubit(ctl) 65 | try self._check_qubit(tgt) 66 | try QuantumCircuit._check_dups([ctl, tgt]) 67 | return self._attach(CzGate(ctl, tgt, self)) as! CzGate 68 | } 69 | } 70 | 71 | extension CompositeGate { 72 | 73 | /** 74 | Apply CZ to circuit. 75 | */ 76 | @discardableResult 77 | public func cz(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CzGate { 78 | try self._check_qubit(ctl) 79 | try self._check_qubit(tgt) 80 | try QuantumCircuit._check_dups([ctl, tgt]) 81 | return self._attach(CzGate(ctl, tgt, self.circuit)) as! CzGate 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CX.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | controlled-NOT gate. 20 | */ 21 | public final class CnotGate: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("cx", [], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return CnotGate(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return self._qasmif("\(name) \(self.args[0].identifier),\(self.args[1].identifier)") 39 | } 40 | 41 | /** 42 | Invert this gate. 43 | */ 44 | @discardableResult 45 | public func inverse() -> CnotGate { 46 | return self 47 | } 48 | 49 | /** 50 | Reapply this gate to corresponding qubits in circ. 51 | */ 52 | public func reapply(_ circ: QuantumCircuit) throws { 53 | try self._modifiers(circ.cx(self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 54 | } 55 | } 56 | 57 | extension QuantumCircuit { 58 | 59 | /** 60 | Apply CNOT from ctl to tgt. 61 | */ 62 | @discardableResult 63 | public func cx(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CnotGate { 64 | try self._check_qubit(ctl) 65 | try self._check_qubit(tgt) 66 | try QuantumCircuit._check_dups([ctl, tgt]) 67 | return self._attach(CnotGate(ctl, tgt, self)) as! CnotGate 68 | } 69 | } 70 | 71 | extension CompositeGate { 72 | 73 | /** 74 | Apply CNOT from ctl to tgt. 75 | */ 76 | @discardableResult 77 | public func cx(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CnotGate { 78 | try self._check_qubit(ctl) 79 | try self._check_qubit(tgt) 80 | try QuantumCircuit._check_dups([ctl, tgt]) 81 | return self._attach(CnotGate(ctl, tgt, self.circuit)) as! CnotGate 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CXBase.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | Fundamental controlled-NOT gate. 20 | */ 21 | public final class CXBase: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("CX", [], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return CXBase(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return self._qasmif("\(name) \(self.args[0].identifier),\(self.args[1].identifier)") 39 | } 40 | 41 | /** 42 | Invert this gate. 43 | */ 44 | @discardableResult 45 | public func inverse() -> CXBase { 46 | return self 47 | } 48 | 49 | /** 50 | Reapply this gate to corresponding qubits in circ. 51 | */ 52 | public func reapply(_ circ: QuantumCircuit) throws { 53 | try self._modifiers(circ.cx_base(self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 54 | } 55 | } 56 | 57 | extension QuantumCircuit { 58 | 59 | /** 60 | Apply CX from ctl, tgt. 61 | */ 62 | @discardableResult 63 | public func cx_base(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CXBase { 64 | try self._check_qubit(ctl) 65 | try self._check_qubit(tgt) 66 | try QuantumCircuit._check_dups([ctl, tgt]) 67 | return self._attach(CXBase(ctl, tgt, self)) as! CXBase 68 | } 69 | } 70 | 71 | extension CompositeGate { 72 | 73 | /** 74 | Apply CX from ctl, tgt. 75 | */ 76 | @discardableResult 77 | public func cx_base(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CXBase { 78 | try self._check_qubit(ctl) 79 | try self._check_qubit(tgt) 80 | try QuantumCircuit._check_dups([ctl, tgt]) 81 | return self._attach(CXBase(ctl, tgt, self.circuit)) as! CXBase 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/ibmqx2/011_3_qubit_grover_50_.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // Name of Experiment: 011 3 qubit grover 50% v1 17 | // Description: A 3 qubit grover amplification repeated twice 18 | struct Q011_3_qubit_grover_50 { 19 | static let QASM = """ 20 | OPENQASM 2.0; 21 | include "qelib1.inc"; 22 | 23 | qreg q[5]; 24 | creg c[5]; 25 | 26 | h q[0]; 27 | h q[1]; 28 | h q[2]; 29 | s q[0]; 30 | s q[1]; 31 | s q[2]; 32 | cx q[1],q[2]; 33 | tdg q[2]; 34 | cx q[0],q[2]; 35 | t q[2]; 36 | cx q[1],q[2]; 37 | tdg q[2]; 38 | cx q[0],q[2]; 39 | t q[1]; 40 | t q[2]; 41 | cx q[1],q[2]; 42 | h q[1]; 43 | h q[2]; 44 | cx q[1],q[2]; 45 | h q[1]; 46 | h q[2]; 47 | cx q[1],q[2]; 48 | cx q[0],q[2]; 49 | t q[0]; 50 | h q[1]; 51 | tdg q[2]; 52 | cx q[0],q[2]; 53 | s q[0]; 54 | s q[1]; 55 | s q[2]; 56 | h q[0]; 57 | h q[1]; 58 | h q[2]; 59 | x q[0]; 60 | x q[1]; 61 | x q[2]; 62 | cx q[1],q[2]; 63 | tdg q[2]; 64 | cx q[0],q[2]; 65 | t q[2]; 66 | cx q[1],q[2]; 67 | tdg q[2]; 68 | cx q[0],q[2]; 69 | t q[1]; 70 | t q[2]; 71 | cx q[1],q[2]; 72 | h q[1]; 73 | h q[2]; 74 | cx q[1],q[2]; 75 | h q[1]; 76 | h q[2]; 77 | cx q[1],q[2]; 78 | cx q[0],q[2]; 79 | t q[0]; 80 | h q[1]; 81 | tdg q[2]; 82 | cx q[0],q[2]; 83 | x q[0]; 84 | x q[1]; 85 | x q[2]; 86 | h q[0]; 87 | h q[1]; 88 | h q[2]; 89 | s q[0]; 90 | s q[1]; 91 | s q[2]; 92 | cx q[1],q[2]; 93 | tdg q[2]; 94 | cx q[0],q[2]; 95 | t q[2]; 96 | cx q[1],q[2]; 97 | tdg q[2]; 98 | cx q[0],q[2]; 99 | t q[1]; 100 | t q[2]; 101 | cx q[1],q[2]; 102 | h q[1]; 103 | h q[2]; 104 | cx q[1],q[2]; 105 | h q[1]; 106 | h q[2]; 107 | cx q[1],q[2]; 108 | cx q[0],q[2]; 109 | t q[0]; 110 | h q[1]; 111 | tdg q[2]; 112 | cx q[0],q[2]; 113 | s q[0]; 114 | s q[1]; 115 | s q[2]; 116 | h q[0]; 117 | h q[1]; 118 | h q[2]; 119 | x q[0]; 120 | x q[1]; 121 | x q[2]; 122 | cx q[1],q[2]; 123 | tdg q[2]; 124 | cx q[0],q[2]; 125 | t q[2]; 126 | cx q[1],q[2]; 127 | tdg q[2]; 128 | cx q[0],q[2]; 129 | t q[1]; 130 | t q[2]; 131 | cx q[1],q[2]; 132 | h q[1]; 133 | h q[2]; 134 | cx q[1],q[2]; 135 | h q[1]; 136 | h q[2]; 137 | cx q[1],q[2]; 138 | cx q[0],q[2]; 139 | t q[0]; 140 | h q[1]; 141 | tdg q[2]; 142 | cx q[0],q[2]; 143 | x q[0]; 144 | x q[1]; 145 | x q[2]; 146 | h q[0]; 147 | h q[1]; 148 | h q[2]; 149 | measure q[0] -> c[0]; 150 | measure q[1] -> c[1]; 151 | measure q[2] -> c[2]; 152 | """ 153 | } 154 | -------------------------------------------------------------------------------- /Sources/qiskit/network/SessionDelegate.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | final class SessionDelegate : NSObject, URLSessionDelegate { 19 | 20 | let credentials: Credentials 21 | 22 | init(_ credentials: Credentials) { 23 | self.credentials = credentials 24 | } 25 | 26 | func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { 27 | 28 | SDKLogger.logInfo("Did receive challenge") 29 | 30 | guard let username = self.credentials.ntlm_credentials["username"], 31 | let password = self.credentials.ntlm_credentials["password"] else { 32 | SDKLogger.logInfo("Challenge missing username password") 33 | #if os(Linux) 34 | challenge.sender?.performDefaultHandling(for: challenge) 35 | #else 36 | challenge.sender?.performDefaultHandling?(for: challenge) 37 | #endif 38 | completionHandler(.performDefaultHandling, nil) 39 | return 40 | } 41 | 42 | guard challenge.previousFailureCount == 0 else { 43 | SDKLogger.logInfo("Challeng too many failures") 44 | #if os(Linux) 45 | challenge.sender?.performDefaultHandling(for: challenge) 46 | #else 47 | challenge.sender?.performDefaultHandling?(for: challenge) 48 | #endif 49 | completionHandler(.performDefaultHandling, nil) 50 | return 51 | } 52 | 53 | guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM else { 54 | SDKLogger.logInfo("Unknown authentication method \(challenge.protectionSpace.authenticationMethod)") 55 | #if os(Linux) 56 | challenge.sender?.performDefaultHandling(for: challenge) 57 | #else 58 | challenge.sender?.performDefaultHandling?(for: challenge) 59 | #endif 60 | completionHandler(.performDefaultHandling, nil) 61 | return 62 | } 63 | let credentials = URLCredential(user: username, password: password, persistence: .forSession) 64 | challenge.sender?.use(credentials, for: challenge) 65 | completionHandler(.useCredential, credentials) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/Swap.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | SWAP Gate. 20 | */ 21 | public final class SwapGate: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) throws { 26 | self.instructionComponent = InstructionComponent("swap", [], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return SwapGate(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | return self._qasmif("\(name) \(self.args[0].identifier),\(self.args[1].identifier)") 39 | } 40 | 41 | /** 42 | Invert this gate. 43 | */ 44 | @discardableResult 45 | public func inverse() -> SwapGate { 46 | return self 47 | } 48 | 49 | /** 50 | Reapply this gate to corresponding qubits in circ. 51 | */ 52 | public func reapply(_ circ: QuantumCircuit) throws { 53 | try self._modifiers(circ.swap(self.args[0] as! QuantumRegisterTuple, 54 | self.args[1] as! QuantumRegisterTuple)) 55 | } 56 | } 57 | 58 | extension QuantumCircuit { 59 | 60 | /** 61 | Apply SWAP from ctl to tgt. 62 | */ 63 | @discardableResult 64 | public func swap(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> SwapGate { 65 | try self._check_qubit(ctl) 66 | try self._check_qubit(tgt) 67 | try QuantumCircuit._check_dups([ctl, tgt]) 68 | return try self._attach(SwapGate(ctl, tgt, self)) as! SwapGate 69 | } 70 | } 71 | 72 | extension CompositeGate { 73 | 74 | /** 75 | Apply SWAP from ctl to tgt. 76 | */ 77 | @discardableResult 78 | public func swap(_ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> SwapGate { 79 | try self._check_qubit(ctl) 80 | try self._check_qubit(tgt) 81 | try QuantumCircuit._check_dups([ctl, tgt]) 82 | return try self._attach(SwapGate(ctl, tgt, self.circuit)) as! SwapGate 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /qiskit.xcodeproj/xcshareddata/xcschemes/qiskitiOS.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 31 | 32 | 33 | 34 | 35 | 36 | 47 | 48 | 54 | 55 | 56 | 57 | 58 | 59 | 65 | 66 | 72 | 73 | 74 | 75 | 77 | 78 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Tests/qiskitTests/qasm/ibmqx2/qe_qft_5.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | // quantum Fourier transform for the Quantum Experience 17 | // on all 5 qubits 18 | struct QE_QFT_5 { 19 | static let QASM = """ 20 | OPENQASM 2.0; 21 | include "qelib1.inc"; 22 | 23 | // Register declarations 24 | qreg q[5]; 25 | creg c[5]; 26 | 27 | // Choose starting state 28 | // ** Put your code here ** 29 | 30 | // Quantum Fourier transform 31 | 32 | // 0, 1, 2, 3, 4 33 | h q[0]; 34 | // cu1(pi/2) q[0],q[1]; 35 | u1(pi/4) q[0]; 36 | cx q[0],q[1]; 37 | u1(-pi/4) q[1]; 38 | cx q[0],q[1]; 39 | u1(pi/4) q[1]; 40 | // end cu1 41 | h q[1]; 42 | // cu1(pi/4) q[0],q[2]; 43 | u1(pi/8) q[0]; 44 | cx q[0],q[2]; 45 | u1(-pi/8) q[2]; 46 | cx q[0],q[2]; 47 | u1(pi/8) q[2]; 48 | // end cu1 49 | // cu1(pi/2) q[1],q[2]; 50 | u1(pi/4) q[1]; 51 | cx q[1],q[2]; 52 | u1(-pi/4) q[2]; 53 | cx q[1],q[2]; 54 | u1(pi/4) q[2]; 55 | // end cu1 56 | h q[2]; 57 | // cu1swp(pi/2) q[3],q[2]; 58 | cx q[3],q[2]; 59 | h q[3]; h q[2]; 60 | cx q[3],q[2]; 61 | h q[3]; h q[2]; 62 | u1(-pi/4) q[2]; 63 | cx q[3],q[2]; 64 | u1(pi/4) q[3]; 65 | u1(pi/4) q[2]; 66 | // end cu1swp 67 | // 0, 1, 3, 2, 4 68 | // cu1(pi/8) q[0],q[2]; // logically q[0],q[3] 69 | u1(pi/16) q[0]; 70 | cx q[0],q[2]; 71 | u1(-pi/16) q[2]; 72 | cx q[0],q[2]; 73 | u1(pi/16) q[2]; 74 | // end cu1 75 | // cu1(pi/4) q[1],q[2]; // logically q[1],q[3] 76 | u1(pi/8) q[1]; 77 | cx q[1],q[2]; 78 | u1(-pi/8) q[2]; 79 | cx q[1],q[2]; 80 | u1(pi/8) q[2]; 81 | // end cu1 82 | h q[2]; // logically q[3] 83 | // cu1swp(pi/2) q[4],q[2]; // logically q[4],q[3] 84 | cx q[4],q[2]; 85 | h q[4]; h q[2]; 86 | cx q[4],q[2]; 87 | h q[4]; h q[2]; 88 | u1(-pi/4) q[2]; 89 | cx q[4],q[2]; 90 | u1(pi/4) q[4]; 91 | u1(pi/4) q[2]; 92 | // end cu1swp 93 | // 0, 1, 4, 2, 3 94 | // cu1(pi/16) q[0],q[2]; // logically q[0],q[4] 95 | u1(pi/32) q[0]; 96 | cx q[0],q[2]; 97 | u1(-pi/32) q[2]; 98 | cx q[0],q[2]; 99 | u1(pi/32) q[2]; 100 | // end cu1 101 | // cu1(pi/8) q[1],q[2]; // logically q[1],q[4] 102 | u1(pi/16) q[1]; 103 | cx q[1],q[2]; 104 | u1(-pi/16) q[2]; 105 | cx q[1],q[2]; 106 | u1(pi/16) q[2]; 107 | // end cu1 108 | // cu1(pi/4) q[3],q[2]; // logically q[2],q[4] 109 | u1(pi/8) q[3]; 110 | cx q[3],q[2]; 111 | u1(-pi/8) q[2]; 112 | cx q[3],q[2]; 113 | u1(pi/8) q[2]; 114 | // end cu1 115 | h q[2]; // logically q[4] 116 | 117 | // Outputs are q[2], q[4], q[3], q[1], q[0] 118 | 119 | // Choose measurement 120 | // ** Put your code here ** 121 | 122 | measure q -> c; 123 | """ 124 | } 125 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CRZ.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | controlled-RZ gate. 20 | */ 21 | public final class CrzGate: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ theta: Double, _ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("crz", [theta], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return CrzGate(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | let theta = self.params[0].format(15) 39 | return self._qasmif("\(name)(\(theta)) \(self.args[0].identifier),\(self.args[1].identifier)") 40 | } 41 | 42 | /** 43 | Invert this gate. 44 | */ 45 | @discardableResult 46 | public func inverse() -> CrzGate { 47 | self.instructionComponent.params[0] = -self.instructionComponent.params[0] 48 | return self 49 | } 50 | 51 | /** 52 | Reapply this gate to corresponding qubits in circ. 53 | */ 54 | public func reapply(_ circ: QuantumCircuit) throws { 55 | try self._modifiers(circ.crz(self.params[0],self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 56 | } 57 | } 58 | 59 | extension QuantumCircuit { 60 | 61 | /** 62 | pply crz from ctl to tgt with angle theta. 63 | */ 64 | @discardableResult 65 | public func crz(_ theta: Double, _ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CrzGate { 66 | try self._check_qubit(ctl) 67 | try self._check_qubit(tgt) 68 | try QuantumCircuit._check_dups([ctl, tgt]) 69 | return self._attach(CrzGate(theta,ctl, tgt, self)) as! CrzGate 70 | } 71 | } 72 | 73 | extension CompositeGate { 74 | 75 | /** 76 | Apply crz from ctl to tgt with angle theta. 77 | */ 78 | @discardableResult 79 | public func crz(_ theta: Double, _ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> CrzGate { 80 | try self._check_qubit(ctl) 81 | try self._check_qubit(tgt) 82 | try QuantumCircuit._check_dups([ctl, tgt]) 83 | return self._attach(CrzGate(theta,ctl, tgt, self.circuit)) as! CrzGate 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/CU1.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | import Foundation 17 | 18 | /** 19 | controlled-U1 gate. 20 | */ 21 | public final class Cu1Gate: Gate, CopyableInstruction { 22 | 23 | public let instructionComponent: InstructionComponent 24 | 25 | fileprivate init(_ theta: Double, _ ctl: QuantumRegisterTuple,_ tgt: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 26 | self.instructionComponent = InstructionComponent("cu1", [theta], [ctl,tgt], circuit) 27 | } 28 | 29 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 30 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 31 | } 32 | 33 | func copy(_ c: QuantumCircuit) -> Instruction { 34 | return Cu1Gate(self.name, self.params, self.args, c) 35 | } 36 | 37 | public var description: String { 38 | let theta = self.params[0].format(15) 39 | return self._qasmif("\(name)(\(theta)) \(self.args[0].identifier),\(self.args[1].identifier)") 40 | } 41 | 42 | /** 43 | Invert this gate. 44 | */ 45 | @discardableResult 46 | public func inverse() -> Cu1Gate { 47 | self.instructionComponent.params[0] = -self.instructionComponent.params[0] 48 | return self 49 | } 50 | 51 | /** 52 | Reapply this gate to corresponding qubits in circ. 53 | */ 54 | public func reapply(_ circ: QuantumCircuit) throws { 55 | try self._modifiers(circ.cu1(self.params[0],self.args[0] as! QuantumRegisterTuple, self.args[1] as! QuantumRegisterTuple)) 56 | } 57 | } 58 | 59 | extension QuantumCircuit { 60 | 61 | /** 62 | pply crz from ctl to tgt with angle theta. 63 | */ 64 | @discardableResult 65 | public func cu1(_ theta: Double, _ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> Cu1Gate { 66 | try self._check_qubit(ctl) 67 | try self._check_qubit(tgt) 68 | try QuantumCircuit._check_dups([ctl, tgt]) 69 | return self._attach(Cu1Gate(theta,ctl, tgt, self)) as! Cu1Gate 70 | } 71 | } 72 | 73 | extension CompositeGate { 74 | 75 | /** 76 | Apply crz from ctl to tgt with angle theta. 77 | */ 78 | @discardableResult 79 | public func cu1(_ theta: Double, _ ctl: QuantumRegisterTuple, _ tgt: QuantumRegisterTuple) throws -> Cu1Gate { 80 | try self._check_qubit(ctl) 81 | try self._check_qubit(tgt) 82 | try QuantumCircuit._check_dups([ctl, tgt]) 83 | return self._attach(Cu1Gate(theta,ctl, tgt, self.circuit)) as! Cu1Gate 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Sources/qiskit/extensions/Z.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2017 IBM RESEARCH. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // ============================================================================= 15 | 16 | 17 | import Foundation 18 | 19 | /** 20 | Pauli Z (phase-flip) gate. 21 | */ 22 | public final class ZGate: Gate, CopyableInstruction { 23 | 24 | public let instructionComponent: InstructionComponent 25 | 26 | fileprivate init(_ qreg: QuantumRegisterTuple, _ circuit: QuantumCircuit) { 27 | self.instructionComponent = InstructionComponent("z", [], [qreg], circuit) 28 | } 29 | 30 | private init(_ name: String, _ params: [Double], _ args: [RegisterArgument], _ circuit: QuantumCircuit) { 31 | self.instructionComponent = InstructionComponent(name, params, args, circuit) 32 | } 33 | 34 | func copy(_ c: QuantumCircuit) -> Instruction { 35 | return ZGate(self.name, self.params, self.args, c) 36 | } 37 | 38 | public var description: String { 39 | return self._qasmif("\(name) \(self.args[0].identifier)") 40 | } 41 | 42 | /** 43 | Invert this gate. 44 | */ 45 | @discardableResult 46 | public func inverse() -> ZGate { 47 | return self 48 | } 49 | 50 | /** 51 | Reapply this gate to corresponding qubits in circ. 52 | */ 53 | public func reapply(_ circ: QuantumCircuit) throws { 54 | try self._modifiers(circ.z(self.args[0] as! QuantumRegisterTuple)) 55 | } 56 | } 57 | 58 | extension QuantumCircuit { 59 | 60 | /** 61 | Apply z to q. 62 | */ 63 | public func z(_ q: QuantumRegister) throws -> InstructionSet { 64 | let gs = InstructionSet() 65 | for j in 0.. ZGate { 76 | try self._check_qubit(q) 77 | return self._attach(ZGate(q, self)) as! ZGate 78 | } 79 | } 80 | 81 | extension CompositeGate { 82 | 83 | /** 84 | Apply z to q. 85 | */ 86 | public func z(_ q: QuantumRegister) throws -> InstructionSet { 87 | let gs = InstructionSet() 88 | for j in 0.. ZGate { 99 | try self._check_qubit(q) 100 | return self._attach(ZGate(q, self.circuit)) as! ZGate 101 | } 102 | } 103 | --------------------------------------------------------------------------------