Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into swernli/python-dumps-…
Browse files Browse the repository at this point in the history
…access
  • Loading branch information
swernli committed Dec 11, 2024
2 parents 48eae22 + b43d102 commit 20fa43a
Show file tree
Hide file tree
Showing 470 changed files with 3,203 additions and 2,264 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ log = "0.4"
miette = { version = "7.2", features = ["fancy-no-syscall"] }
thiserror = "1.0"
nalgebra = { version = "0.33" }
ndarray = "0.15.4"
num-bigint = "0.4"
num-complex = "0.4"
num-traits = "0.2"
Expand All @@ -77,7 +78,7 @@ wasm-bindgen-futures = "0.4"
rand = "0.8"
serde_json = "1.0"
pyo3 = "0.22"
quantum-sparse-sim = { git = "https://github.com/qir-alliance/qir-runner", tag = "v0.7.4" }
quantum-sparse-sim = { git = "https://github.com/qir-alliance/qir-runner", rev = "562e2c11ad685dd01bfc1ae975e00d4133615995" }
async-trait = "0.1"
tokio = { version = "1.35", features = ["macros", "rt"] }

Expand Down
2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def run_ci_historic_benchmark():
"ipykernel",
"nbconvert",
"pandas",
"qiskit>=1.2.2,<2.0.0",
"qiskit>=1.3.0,<2.0.0",
]
subprocess.run(pip_install_args, check=True, text=True, cwd=root_dir, env=pip_env)

Expand Down
3 changes: 1 addition & 2 deletions compiler/qsc_doc_gen/src/generate_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,9 @@ fn generate_exported_file_content(
// Note: we are assuming the package kind does not change
let true_metadata = get_metadata(package_kind, true_ns.clone(), true_item, display)?;
let true_fqn = true_metadata.fully_qualified_name();
let name = true_metadata.name;

let summary = format!(
"This is an exported item. The actual definition is found here: [{name}](xref:Qdk.{true_fqn})"
"This is an exported item. The actual definition is found here: [{true_fqn}](xref:Qdk.{true_fqn})"
);

metadata.summary = summary.clone();
Expand Down
6 changes: 3 additions & 3 deletions compiler/qsc_doc_gen/src/generate_docs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ fn redirect_generation() {
---
uid: Qdk.Microsoft.Quantum.Core.Length
title: Length exported item
description: "Q# Length exported item: This is an exported item. The actual definition is found here: [Length](xref:Qdk.Std.Core.Length)"
description: "Q# Length exported item: This is an exported item. The actual definition is found here: [Std.Core.Length](xref:Qdk.Std.Core.Length)"
ms.date: {TIMESTAMP}
ms.topic: managed-reference
qsharp.kind: export
qsharp.package: __Std__
qsharp.namespace: Microsoft.Quantum.Core
qsharp.name: Length
qsharp.summary: "This is an exported item. The actual definition is found here: [Length](xref:Qdk.Std.Core.Length)"
qsharp.summary: "This is an exported item. The actual definition is found here: [Std.Core.Length](xref:Qdk.Std.Core.Length)"
---
# Length exported item
Fully qualified name: Microsoft.Quantum.Core.Length
This is an exported item. The actual definition is found here: [Length](xref:Qdk.Std.Core.Length)
This is an exported item. The actual definition is found here: [Std.Core.Length](xref:Qdk.Std.Core.Length)
"#]]
.assert_eq(full_contents.as_str());
}
Expand Down
1 change: 1 addition & 0 deletions compiler/qsc_eval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license.workspace = true

[dependencies]
miette = { workspace = true }
ndarray = { workspace = true }
num-bigint = { workspace = true }
num-complex = { workspace = true }
num-traits = { workspace = true }
Expand Down
47 changes: 46 additions & 1 deletion compiler/qsc_eval/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::noise::PauliNoise;
use crate::val::Value;
use crate::{noise::PauliNoise, val::unwrap_tuple};
use ndarray::Array2;
use num_bigint::BigUint;
use num_complex::Complex;
use quantum_sparse_sim::QuantumSim;
Expand Down Expand Up @@ -419,6 +420,29 @@ impl Backend for SparseSim {
self.apply_noise(q);
Some(Ok(Value::unit()))
}
"Apply" => {
let [matrix, qubits] = unwrap_tuple(arg);
let qubits = qubits
.unwrap_array()
.iter()
.filter_map(|q| q.clone().unwrap_qubit().try_deref().map(|q| q.0))
.collect::<Vec<_>>();
let matrix = unwrap_matrix_as_array2(matrix, &qubits);

// Confirm the matrix is unitary by checking if multiplying it by its adjoint gives the identity matrix (up to numerical precision).
let adj = matrix.t().map(Complex::<f64>::conj);
if (matrix.dot(&adj) - Array2::<Complex<f64>>::eye(1 << qubits.len()))
.map(|x| x.norm())
.sum()
> 1e-9
{
return Some(Err("matrix is not unitary".to_string()));
}

self.sim.apply(&matrix, &qubits, None);

Some(Ok(Value::unit()))
}
_ => None,
}
}
Expand All @@ -438,6 +462,27 @@ impl Backend for SparseSim {
}
}

fn unwrap_matrix_as_array2(matrix: Value, qubits: &[usize]) -> Array2<Complex<f64>> {
let matrix: Vec<Vec<Complex<f64>>> = matrix
.unwrap_array()
.iter()
.map(|row| {
row.clone()
.unwrap_array()
.iter()
.map(|elem| {
let [re, im] = unwrap_tuple(elem.clone());
Complex::<f64>::new(re.unwrap_double(), im.unwrap_double())
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();

Array2::from_shape_fn((1 << qubits.len(), 1 << qubits.len()), |(i, j)| {
matrix[i][j]
})
}

/// Simple struct that chains two backends together so that the chained
/// backend is called before the main backend.
/// For any intrinsics that return a value,
Expand Down
8 changes: 1 addition & 7 deletions compiler/qsc_eval/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ use crate::{
backend::Backend,
error::PackageSpan,
output::Receiver,
val::{self, Value},
val::{self, unwrap_tuple, Value},
Error, Rc,
};
use num_bigint::BigInt;
use rand::{rngs::StdRng, Rng};
use rustc_hash::{FxHashMap, FxHashSet};
use std::array;
use std::convert::TryFrom;

#[allow(clippy::too_many_lines)]
Expand Down Expand Up @@ -426,8 +425,3 @@ pub fn qubit_relabel(

Ok(Value::unit())
}

fn unwrap_tuple<const N: usize>(value: Value) -> [Value; N] {
let values = value.unwrap_tuple();
array::from_fn(|i| values[i].clone())
}
7 changes: 7 additions & 0 deletions compiler/qsc_eval/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use num_bigint::BigInt;
use qsc_data_structures::{display::join, functors::FunctorApp};
use qsc_fir::fir::{Functor, Pauli, StoreItemId};
use std::{
array,
fmt::{self, Display, Formatter},
rc::{Rc, Weak},
};
Expand Down Expand Up @@ -578,3 +579,9 @@ pub fn update_functor_app(functor: Functor, app: FunctorApp) -> FunctorApp {
},
}
}

#[must_use]
pub fn unwrap_tuple<const N: usize>(value: Value) -> [Value; N] {
let values = value.unwrap_tuple();
array::from_fn(|i| values[i].clone())
}
2 changes: 1 addition & 1 deletion compiler/qsc_frontend/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub(super) fn lift(
name: Ident {
id: assigner.next_node(),
span,
name: "lambda".into(),
name: "<lambda>".into(),
},
generics: Vec::new(),
input,
Expand Down
4 changes: 3 additions & 1 deletion compiler/qsc_frontend/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use self::convert::TyConversionError;
#[derive(Clone, Debug, Diagnostic, Error)]
pub(super) enum Error {
#[error("unknown attribute {0}")]
#[diagnostic(help("supported attributes are: EntryPoint, Config"))]
#[diagnostic(help(
"supported attributes are: EntryPoint, Config, SimulatableIntrinsic, Measurement, Reset"
))]
#[diagnostic(code("Qsc.LowerAst.UnknownAttr"))]
UnknownAttr(String, #[label] Span),
#[error("invalid attribute arguments: expected {0}")]
Expand Down
32 changes: 16 additions & 16 deletions compiler/qsc_frontend/src/lower/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ fn lambda_function_empty_closure() {
Item 2 [57-67] (Internal):
Parent: 1
Callable 15 [57-67] (function):
name: Ident 16 [57-67] "lambda"
name: Ident 16 [57-67] "<lambda>"
input: Pat 14 [57-67] [Type (Int,)]: Tuple:
Pat 9 [57-58] [Type Int]: Bind: Ident 10 [57-58] "x"
output: Int
Expand Down Expand Up @@ -1025,7 +1025,7 @@ fn lambda_function_empty_closure_passed() {
Item 3 [93-103] (Internal):
Parent: 2
Callable 25 [93-103] (function):
name: Ident 26 [93-103] "lambda"
name: Ident 26 [93-103] "<lambda>"
input: Pat 24 [93-103] [Type (Int,)]: Tuple:
Pat 19 [93-94] [Type Int]: Bind: Ident 20 [93-94] "x"
output: Int
Expand Down Expand Up @@ -1081,7 +1081,7 @@ fn lambda_function_closure() {
Item 2 [76-86] (Internal):
Parent: 1
Callable 21 [76-86] (function):
name: Ident 22 [76-86] "lambda"
name: Ident 22 [76-86] "<lambda>"
input: Pat 19 [76-86] [Type (Int, Int)]: Tuple:
Pat 20 [53-54] [Type Int]: Bind: Ident 18 [53-54] "x"
Pat 13 [76-77] [Type Int]: Bind: Ident 14 [76-77] "y"
Expand Down Expand Up @@ -1138,7 +1138,7 @@ fn lambda_function_closure_repeated_var() {
Item 2 [76-90] (Internal):
Parent: 1
Callable 23 [76-90] (function):
name: Ident 24 [76-90] "lambda"
name: Ident 24 [76-90] "<lambda>"
input: Pat 21 [76-90] [Type (Int, Int)]: Tuple:
Pat 22 [53-54] [Type Int]: Bind: Ident 20 [53-54] "x"
Pat 13 [76-77] [Type Int]: Bind: Ident 14 [76-77] "y"
Expand Down Expand Up @@ -1209,7 +1209,7 @@ fn lambda_function_closure_passed() {
Item 3 [120-130] (Internal):
Parent: 2
Callable 31 [120-130] (function):
name: Ident 32 [120-130] "lambda"
name: Ident 32 [120-130] "<lambda>"
input: Pat 29 [120-130] [Type (Int, Int)]: Tuple:
Pat 30 [101-102] [Type Int]: Bind: Ident 28 [101-102] "x"
Pat 23 [120-121] [Type Int]: Bind: Ident 24 [120-121] "y"
Expand Down Expand Up @@ -1283,7 +1283,7 @@ fn lambda_function_nested_closure() {
Item 3 [172-190] (Internal):
Parent: 2
Callable 51 [172-190] (function):
name: Ident 52 [172-190] "lambda"
name: Ident 52 [172-190] "<lambda>"
input: Pat 47 [172-190] [Type (Int, Int, Int, Int)]: Tuple:
Pat 48 [111-112] [Type Int]: Bind: Ident 44 [111-112] "a"
Pat 49 [130-131] [Type Int]: Bind: Ident 45 [130-131] "b"
Expand All @@ -1306,7 +1306,7 @@ fn lambda_function_nested_closure() {
Item 4 [130-200] (Internal):
Parent: 2
Callable 59 [130-200] (function):
name: Ident 60 [130-200] "lambda"
name: Ident 60 [130-200] "<lambda>"
input: Pat 57 [130-200] [Type (Int, Int)]: Tuple:
Pat 58 [111-112] [Type Int]: Bind: Ident 56 [111-112] "a"
Pat 25 [130-131] [Type Int]: Bind: Ident 26 [130-131] "b"
Expand Down Expand Up @@ -1382,7 +1382,7 @@ fn lambda_operation_empty_closure() {
Item 3 [137-144] (Internal):
Parent: 2
Callable 27 [137-144] (operation):
name: Ident 28 [137-144] "lambda"
name: Ident 28 [137-144] "<lambda>"
input: Pat 26 [137-144] [Type (Qubit,)]: Tuple:
Pat 23 [137-138] [Type Qubit]: Bind: Ident 24 [137-138] "q"
output: Unit
Expand Down Expand Up @@ -1465,7 +1465,7 @@ fn lambda_operation_closure() {
Item 4 [199-215] (Internal):
Parent: 3
Callable 35 [199-215] (operation):
name: Ident 36 [199-215] "lambda"
name: Ident 36 [199-215] "<lambda>"
input: Pat 33 [199-215] [Type (Qubit, Unit)]: Tuple:
Pat 34 [174-175] [Type Qubit]: Bind: Ident 32 [174-175] "q"
Pat 28 [199-201] [Type Unit]: Unit
Expand Down Expand Up @@ -1543,7 +1543,7 @@ fn lambda_adj() {
Item 4 [138-147] (Internal):
Parent: 3
Callable 27 [138-147] (operation):
name: Ident 28 [138-147] "lambda"
name: Ident 28 [138-147] "<lambda>"
input: Pat 26 [138-147] [Type (Qubit,)]: Tuple:
Pat 21 [138-139] [Type Qubit]: Bind: Ident 22 [138-139] "q"
output: Unit
Expand Down Expand Up @@ -1611,7 +1611,7 @@ fn partial_app_one_hole() {
Item 3 [99-108] (Internal):
Parent: 2
Callable 37 [99-108] (function):
name: Ident 38 [99-108] "lambda"
name: Ident 38 [99-108] "<lambda>"
input: Pat 35 [99-108] [Type (Int, Int)]: Tuple:
Pat 36 [106-107] [Type Int]: Bind: Ident 34 [106-107] "arg"
Pat 24 [103-104] [Type Int]: Bind: Ident 23 [103-104] "hole"
Expand Down Expand Up @@ -1679,7 +1679,7 @@ fn partial_app_two_holes() {
Item 3 [99-108] (Internal):
Parent: 2
Callable 33 [99-108] (function):
name: Ident 34 [99-108] "lambda"
name: Ident 34 [99-108] "<lambda>"
input: Pat 32 [99-108] [Type ((Int, Int),)]: Tuple:
Pat 30 [102-108] [Type (Int, Int)]: Tuple:
Pat 24 [103-104] [Type Int]: Bind: Ident 23 [103-104] "hole"
Expand Down Expand Up @@ -1752,7 +1752,7 @@ fn partial_app_nested_tuple() {
Item 3 [130-152] (Internal):
Parent: 2
Callable 52 [130-152] (function):
name: Ident 53 [130-152] "lambda"
name: Ident 53 [130-152] "<lambda>"
input: Pat 50 [130-152] [Type (Double, (Int, (Bool, String), Result))]: Tuple:
Pat 51 [141-144] [Type Double]: Bind: Ident 49 [141-144] "arg"
Pat 47 [133-152] [Type (Int, (Bool, String), Result)]: Tuple:
Expand Down Expand Up @@ -1836,7 +1836,7 @@ fn partial_app_nested_tuple_singleton_unwrap() {
Item 3 [130-155] (Internal):
Parent: 2
Callable 56 [130-155] (function):
name: Ident 57 [130-155] "lambda"
name: Ident 57 [130-155] "<lambda>"
input: Pat 53 [130-155] [Type (Bool, Double, (Int, String, Result))]: Tuple:
Pat 54 [138-142] [Type Bool]: Bind: Ident 51 [138-142] "arg"
Pat 55 [144-147] [Type Double]: Bind: Ident 52 [144-147] "arg"
Expand Down Expand Up @@ -2091,7 +2091,7 @@ fn partial_app_bound_to_non_arrow_ty() {
Item 3 [113-122] (Internal):
Parent: 2
Callable 37 [113-122] (function):
name: Ident 38 [113-122] "lambda"
name: Ident 38 [113-122] "<lambda>"
input: Pat 35 [113-122] [Type (Int, Int)]: Tuple:
Pat 36 [117-118] [Type Int]: Bind: Ident 34 [117-118] "arg"
Pat 30 [120-121] [Type Int]: Bind: Ident 29 [120-121] "hole"
Expand Down Expand Up @@ -2366,7 +2366,7 @@ fn lambda_with_invalid_free_variable() {
Item 2 [52-67] (Internal):
Parent: 1
Callable 12 [52-67] (operation):
name: Ident 13 [52-67] "lambda"
name: Ident 13 [52-67] "<lambda>"
input: Pat 11 [52-67] [Type (Unit,)]: Tuple:
Pat 7 [52-54] [Type Unit]: Unit
output: Unit
Expand Down
Loading

0 comments on commit 20fa43a

Please sign in to comment.