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

Visualizing Quantum State (for Linux) #7

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@

import PackageDescription

var packageDependencies: [Package.Dependency] = [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
]

#if os(Linux)

packageDependencies += [
.package(url: "https://github.com/indisoluble/CWebkitGtk-Linux.git", from: "1.0.0"),
.package(url: "https://github.com/indisoluble/CLapacke-Linux.git", from: "1.0.0"),
]

#endif

let package = Package(
name: "qiskit",
products: [
Expand All @@ -13,10 +27,7 @@ let package = Package(
.executable(
name: "qiskitexamples", targets: ["examples"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
dependencies: packageDependencies,
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
Expand Down
30 changes: 26 additions & 4 deletions Sources/qiskit/datastructures/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import Accelerate

#elseif os(Linux)

import CLapacke_Linux

#endif

import Foundation
Expand Down Expand Up @@ -453,8 +457,6 @@ extension Matrix where T == Complex {
return m
}

#if os(OSX) || os(iOS)

public func eigh() throws -> (Vector<Double>, [Vector<Complex>]) {
guard isHermitian else {
throw ArrayError.matrixIsNotHermitian
Expand All @@ -468,6 +470,9 @@ extension Matrix where T == Complex {
var info = Int32()

var w = Array(repeating: Double(), count: rowCount)

#if os(OSX) || os(iOS)

var a = flattenCol().map { __CLPK_doublecomplex(r: $0.real, i: $0.imag) }

// Get optimal workspace
Expand All @@ -490,19 +495,36 @@ extension Matrix where T == Complex {

zheevd_(&jobz, &uplo, &n, &a, &lda, &w, &work, &lengthWork, &rWork, &lengthRWork, &iWork, &lengthIWork, &info)

#elseif os(Linux)

var a = flattenCol().value
let aPointer = UnsafeMutablePointer(mutating: a)
let aOpaquePointer = OpaquePointer(aPointer)

info = LAPACKE_zheevd(LAPACK_COL_MAJOR, jobz, uplo, n, aOpaquePointer, lda, &w)

#endif

// Validate results
if (info > 0) {
throw ArrayError.unableToComputeEigenValues
}

#if os(OSX) || os(iOS)

let aComplex = MultiDArray<Complex>(value: a.map { Complex($0.r, $0.i) })

#elseif os(Linux)

let aComplex = MultiDArray<Complex>(value: a)

#endif

let vectorsStoredByCol = try aComplex.reshape([rowCount, rowCount]).value as! [[Complex]]

return (Vector(value: w), vectorsStoredByCol.map { Vector(value: $0) })
}

#endif

public func sqrt() -> Matrix {
var m = Matrix<T>(repeating: 0, rows: self.rowCount, cols: self.colCount)
for row in 0..<m.rowCount {
Expand Down
14 changes: 5 additions & 9 deletions Sources/qiskit/tools/visualization/Visualization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

import Foundation

// MARK: - Public types
Expand Down Expand Up @@ -44,7 +42,7 @@ public func plot_histogram(_ counts: [String : Int],
values: probabilities,
configuration: configuration)

return AppleWebViewFactory.makeWebView(size: size, html: html)
return WebViewFactory.makeWebView(size: size, html: html)
}

public func plot_state(_ rho: Matrix<Complex>,
Expand Down Expand Up @@ -81,7 +79,7 @@ private func plot_state_city(_ rho: Matrix<Complex>,
realValues: realValues,
imagValues: imagValues)

return AppleWebViewFactory.makeWebView(size: size, html: html)
return WebViewFactory.makeWebView(size: size, html: html)
}

private func plot_state_paulivec(_ rho: Matrix<Complex>,
Expand All @@ -108,7 +106,7 @@ private func plot_state_paulivec(_ rho: Matrix<Complex>,
values: values,
configuration: configuration)

return AppleWebViewFactory.makeWebView(size: size, html: html)
return WebViewFactory.makeWebView(size: size, html: html)
}

private func pauli_group(_ numberofqubits: Int) -> [Pauli] {
Expand Down Expand Up @@ -146,7 +144,7 @@ private func plot_state_bloch(_ rho: Matrix<Complex>,

let html = StateBlochChartHtmlFactory.makeHtml(blochStates: bloch_states)

return AppleWebViewFactory.makeWebView(size: size, html: html)
return WebViewFactory.makeWebView(size: size, html: html)
}

private func plot_state_qsphere(_ rho: Matrix<Complex>,
Expand Down Expand Up @@ -176,7 +174,7 @@ private func plot_state_qsphere(_ rho: Matrix<Complex>,

let html = StateQsphereChartHtmlFactory.makeHtml(numberOfBits: numberOfBits, series: series)

return AppleWebViewFactory.makeWebView(size: size, html: html)
return WebViewFactory.makeWebView(size: size, html: html)
}

private func matrix_eigh(_ rho: Matrix<Complex>) -> (Vector<Double>, [Vector<Complex>]) {
Expand Down Expand Up @@ -278,5 +276,3 @@ private func phase_to_color_wheel(_ complex: Complex) -> StateQsphereChartValue.

return StateQsphereChartValue.Color(rawValue: angle_round)!
}

#endif
16 changes: 10 additions & 6 deletions Sources/qiskit/tools/visualization/VisualizationTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

#if os(OSX)

import Cocoa

#else
#elseif os(iOS)

import UIKit

#elseif os(Linux)

import CWebkitGtk_Linux

#endif

// MARK: - Public body
Expand All @@ -35,10 +37,14 @@ public struct VisualizationTypes {

public typealias View = NSView

#else
#elseif os(iOS)

public typealias View = UIView

#elseif os(Linux)

public typealias View = UnsafeMutablePointer<GtkWidget>

#endif

public struct Size {
Expand All @@ -56,5 +62,3 @@ public struct VisualizationTypes {
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

// MARK: - Main body

struct BarChartConfiguration {
Expand Down Expand Up @@ -44,5 +42,3 @@ struct BarChartConfiguration {
self.showValueOnTop = showValueOnTop
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

import Foundation

// MARK: - Main body
Expand Down Expand Up @@ -110,5 +108,3 @@ struct BarChartHtmlFactory {
"""
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

import Foundation

// MARK: - Main body
Expand Down Expand Up @@ -157,5 +155,3 @@ struct StateBlochChartHtmlFactory {
"""
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

import Foundation

// MARK: - Main body
Expand Down Expand Up @@ -112,5 +110,3 @@ struct StateCityChartHtmlFactory {
"""
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

import Foundation

// MARK: - Main body
Expand Down Expand Up @@ -160,5 +158,3 @@ struct StateQsphereChartHtmlFactory {
"""
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

// MARK: - Main body

struct StateQsphereChartSerie {
Expand All @@ -24,5 +22,3 @@ struct StateQsphereChartSerie {
let probMix: Double
let values: [StateQsphereChartValue]
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.
// =============================================================================

#if os(OSX) || os(iOS)

// MARK: - Main body

struct StateQsphereChartValue {
Expand Down Expand Up @@ -84,5 +82,3 @@ struct StateQsphereChartValue {
return rgba
}
}

#endif
35 changes: 35 additions & 0 deletions Sources/qiskit/tools/visualization/charts/WebViewFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 IBM RESEARCH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

import Foundation

// MARK: - Main body

struct WebViewFactory {

// MARK: - Public class methods

static func makeWebView(size: VisualizationTypes.Size, html: String) -> VisualizationTypes.View {
#if os(OSX) || os(iOS)

return AppleWebViewFactory.makeWebView(size: size, html: html)

#elseif os(Linux)

return LinuxWebViewFactory.makeWebView(html: html)

#endif
}
}
Loading