Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
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
22 changes: 6 additions & 16 deletions Standard/src/Arithmetic/Asserts.qs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arithmetic {
Expand Down Expand Up @@ -32,14 +32,15 @@ namespace Microsoft.Quantum.Arithmetic {
/// ## tolerance
/// Absolute tolerance on the difference between actual and expected.
///
/// # Remarks
/// ## Example
/// # Example
/// Suppose that the `qubits` register encodes a 3-qubit quantum state
/// $\ket{\psi}=\sqrt{1/8}\ket{0}+\sqrt{7/8}\ket{6}$ in little-endian format.
/// This means that the number states $\ket{0}\equiv\ket{0}\ket{0}\ket{0}$
/// and $\ket{6}\equiv\ket{0}\ket{1}\ket{1}$. Then the following asserts succeed:
/// - `AssertProbInt(0,0.125,qubits,10e-10);`
/// - `AssertProbInt(6,0.875,qubits,10e-10);`
/// ```qsharp
/// AssertProbInt(0, 0.125, qubits, 10e-10);
/// AssertProbInt(6, 0.875, qubits, 10e-10);
/// ```
operation AssertProbInt(stateIndex : Int, expected : Double, qubits : LittleEndian, tolerance : Double) : Unit {
using (flag = Qubit()) {
within {
Expand All @@ -50,17 +51,6 @@ namespace Microsoft.Quantum.Arithmetic {
}
}

operation AssertSignedProbInt(stateIndex : Int, expected : Double, sign : Qubit, qubits : LittleEndian, tolerance : Double) : Unit {
using (flag = Qubit()) {
let signOffset = expected < 0.0 ? 1 <<< Length(qubits!) | 0;
within {
(ControlledOnInt(stateIndex + signOffset, X))(qubits! + [sign], flag);
} apply {
AssertMeasurementProbability([PauliZ], [flag], One, AbsD(expected), $"AssertSignedProbInt failed on stateIndex {stateIndex}, expected probability {expected}.", tolerance);
}
}
}

/// # Summary
/// Asserts that the most significant qubit of a qubit register
/// representing an unsigned integer is in a particular state.
Expand Down
150 changes: 80 additions & 70 deletions Standard/src/Preparation/Arbitrary.qs
Original file line number Diff line number Diff line change
Expand Up @@ -12,101 +12,110 @@ namespace Microsoft.Quantum.Preparation {
// from the computational basis state $\ket{0...0}$.

/// # Summary
/// Returns an operation that prepares the given quantum state.
/// Given a set of coefficients and a little-endian encoded quantum register,
/// prepares an state on that register described by the given coefficients.
///
/// The returned operation $U$ prepares an arbitrary quantum
/// state $\ket{\psi}$ with positive coefficients $\alpha_j\ge 0$ from
/// the $n$-qubit computational basis state $\ket{0...0}$.
/// # Description
/// This operation prepares an arbitrary quantum
/// state $\ket{\psi}$ with complex coefficients $r_j e^{i t_j}$ from
/// the $n$-qubit computational basis state $\ket{0 \cdots 0}$.
/// In particular, the action of this operation can be simulated by the
/// a unitary transformation $U$ which acts on the all-zeros state as
///
/// The action of U on a newly-allocated register is given by
/// $$
/// \begin{align}
/// U \ket{0\cdots 0} = \ket{\psi} = \frac{\sum_{j=0}^{2^n-1}\alpha_j \ket{j}}{\sqrt{\sum_{j=0}^{2^n-1}|\alpha_j|^2}}.
/// U\ket{0...0}
/// & = \ket{\psi} \\\\
/// & = \frac{
/// \sum_{j=0}^{2^n-1} r_j e^{i t_j} \ket{j}
/// }{
/// \sqrt{\sum_{j=0}^{2^n-1} |r_j|^2}
/// }.
/// \end{align}
/// $$
///
/// # Input
/// ## coefficients
/// Array of up to $2^n$ coefficients $\alpha_j$. The $j$th coefficient
/// Array of up to $2^n$ complex coefficients represented by their
/// absolute value and phase $(r_j, t_j)$. The $j$th coefficient
/// indexes the number state $\ket{j}$ encoded in little-endian format.
///
/// # Output
/// A state-preparation unitary operation $U$.
/// ## qubits
/// Qubit register encoding number states in little-endian format. This is
/// expected to be initialized in the computational basis state
/// $\ket{0...0}$.
///
/// # Remarks
/// Negative input coefficients $\alpha_j < 0$ will be treated as though
/// positive with value $|\alpha_j|$. `coefficients` will be padded with
/// elements $\alpha_j = 0.0$ if fewer than $2^n$ are specified.
///
/// ## Example
/// The following snippet prepares the quantum state $\ket{\psi}=\sqrt{1/8}\ket{0}+\sqrt{7/8}\ket{2}$
/// in the qubit register `qubitsLE`.
/// ```qsharp
/// let amplitudes = [Sqrt(0.125), 0.0, Sqrt(0.875), 0.0];
/// let op = StatePreparationPositiveCoefficients(amplitudes);
/// using (qubits = Qubit[2]) {
/// let qubitsLE = LittleEndian(qubits);
/// op(qubitsLE);
/// }
/// ```
function StatePreparationPositiveCoefficients(coefficients : Double[]) : (LittleEndian => Unit is Adj + Ctl) {
let coefficientsComplexPolar = Mapped(Compose(ComplexPolar(_, 0.0), AbsD), coefficients);
return PrepareArbitraryState(coefficientsComplexPolar, _);
/// Negative input coefficients $r_j < 0$ will be treated as though
/// positive with value $|r_j|$. `coefficients` will be padded with
/// elements $(r_j, t_j) = (0.0, 0.0)$ if fewer than $2^n$ are
/// specified.
///
/// # References
/// - Synthesis of Quantum Logic Circuits
/// Vivek V. Shende, Stephen S. Bullock, Igor L. Markov
/// https://arxiv.org/abs/quant-ph/0406176
///
/// # See Also
/// - Microsoft.Quantum.Preparation.ApproximatelyPrepareArbitraryState
operation PrepareArbitraryStateCP(coefficients : ComplexPolar[], qubits : LittleEndian) : Unit is Adj + Ctl {
ApproximatelyPrepareArbitraryStateCP(0.0, coefficients, qubits);
}

/// # Summary
/// Returns an operation that prepares a specific quantum state.
/// Given a set of coefficients and a little-endian encoded quantum register,
/// prepares an state on that register described by the given coefficients.
///
/// The returned operation $U$ prepares an arbitrary quantum
/// # Description
/// This operation prepares an arbitrary quantum
/// state $\ket{\psi}$ with complex coefficients $r_j e^{i t_j}$ from
/// the $n$-qubit computational basis state $\ket{0...0}$.
/// the $n$-qubit computational basis state $\ket{0 \cdots 0}$.
/// In particular, the action of this operation can be simulated by the
/// a unitary transformation $U$ that acts on the all-zeros state as
///
/// The action of U on a newly-allocated register is given by
/// $$
/// \begin{align}
/// U\ket{0...0}=\ket{\psi}=\frac{\sum_{j=0}^{2^n-1}r_j e^{i t_j}\ket{j}}{\sqrt{\sum_{j=0}^{2^n-1}|r_j|^2}}.
/// U\ket{0...0}
/// & = \ket{\psi} \\\\
/// & = \frac{
/// \sum_{j=0}^{2^n-1} r_j e^{i t_j} \ket{j}
/// }{
/// \sqrt{\sum_{j=0}^{2^n-1} |r_j|^2}
/// }.
/// \end{align}
/// $$
///
/// # Input
/// ## coefficients
/// Array of up to $2^n$ complex coefficients represented by their
/// absolute value and phase $(r_j, t_j)$. The $j$th coefficient
/// Array of up to $2^n$ real coefficients. The $j$th coefficient
/// indexes the number state $\ket{j}$ encoded in little-endian format.
///
/// # Output
/// A state-preparation unitary operation $U$.
/// ## qubits
/// Qubit register encoding number states in little-endian format. This is
/// expected to be initialized in the computational basis state
/// $\ket{0...0}$.
///
/// # Remarks
/// Negative input coefficients $r_j < 0$ will be treated as though
/// positive with value $|r_j|$. `coefficients` will be padded with
/// elements $(r_j, t_j) = (0.0, 0.0)$ if fewer than $2^n$ are
/// specified.
///
/// ## Example
/// The following snippet prepares the quantum state $\ket{\psi}=e^{i 0.1}\sqrt{1/8}\ket{0}+\sqrt{7/8}\ket{2}$
/// in the qubit register `qubitsLE`.
/// ```qsharp
/// let amplitudes = [Sqrt(0.125), 0.0, Sqrt(0.875), 0.0];
/// let phases = [0.1, 0.0, 0.0, 0.0];
/// mutable complexNumbers = new ComplexPolar[4];
/// for (idx in 0..3) {
/// set complexNumbers[idx] = ComplexPolar(amplitudes[idx], phases[idx]);
/// }
/// let op = StatePreparationComplexCoefficients(complexNumbers);
/// using (qubits = Qubit[2]) {
/// let qubitsLE = LittleEndian(qubits);
/// op(qubitsLE);
/// }
/// ```
function StatePreparationComplexCoefficients (coefficients : ComplexPolar[]) : (LittleEndian => Unit is Adj + Ctl) {
return PrepareArbitraryState(coefficients, _);
/// # References
/// - Synthesis of Quantum Logic Circuits
/// Vivek V. Shende, Stephen S. Bullock, Igor L. Markov
/// https://arxiv.org/abs/quant-ph/0406176
///
/// # See Also
/// - Microsoft.Quantum.Preparation.ApproximatelyPrepareArbitraryState
operation PrepareArbitraryStateD(coefficients : Double[], qubits : LittleEndian) : Unit is Adj + Ctl {
ApproximatelyPrepareArbitraryStateD(0.0, coefficients, qubits);
}


/// # Summary
/// Given a set of coefficients and a little-endian encoded quantum register,
/// prepares an state on that register described by the given coefficients.
/// prepares an state on that register described by the given coefficients,
/// up to a given approximation tolerance.
///
/// # Description
/// This operation prepares an arbitrary quantum
Expand All @@ -128,6 +137,9 @@ namespace Microsoft.Quantum.Preparation {
/// $$
///
/// # Input
/// ## tolerance
/// The approximation tolerance to be used when preparing the given state.
///
/// ## coefficients
/// Array of up to $2^n$ complex coefficients represented by their
/// absolute value and phase $(r_j, t_j)$. The $j$th coefficient
Expand All @@ -148,11 +160,13 @@ namespace Microsoft.Quantum.Preparation {
/// - Synthesis of Quantum Logic Circuits
/// Vivek V. Shende, Stephen S. Bullock, Igor L. Markov
/// https://arxiv.org/abs/quant-ph/0406176
///
/// # See Also
/// - Microsoft.Quantum.Preparation.ApproximatelyPrepareArbitraryState
operation PrepareArbitraryState(coefficients : ComplexPolar[], qubits : LittleEndian) : Unit is Adj + Ctl {
ApproximatelyPrepareArbitraryState(0.0, coefficients, qubits);
operation ApproximatelyPrepareArbitraryStateCP(
tolerance : Double,
coefficients : ComplexPolar[],
qubits : LittleEndian
)
: Unit is Adj + Ctl {
(_CompileApproximateArbitraryStatePreparation(tolerance, coefficients, Length(qubits!)))(qubits);
}

/// # Summary
Expand Down Expand Up @@ -184,8 +198,7 @@ namespace Microsoft.Quantum.Preparation {
/// The approximation tolerance to be used when preparing the given state.
///
/// ## coefficients
/// Array of up to $2^n$ complex coefficients represented by their
/// absolute value and phase $(r_j, t_j)$. The $j$th coefficient
/// Array of up to $2^n$ real coefficients. The $j$th coefficient
/// indexes the number state $\ket{j}$ encoded in little-endian format.
///
/// ## qubits
Expand All @@ -203,16 +216,14 @@ namespace Microsoft.Quantum.Preparation {
/// - Synthesis of Quantum Logic Circuits
/// Vivek V. Shende, Stephen S. Bullock, Igor L. Markov
/// https://arxiv.org/abs/quant-ph/0406176
///
/// # See Also
/// - Microsoft.Quantum.Preparation.ApproximatelyPrepareArbitraryState
operation ApproximatelyPrepareArbitraryState(
operation ApproximatelyPrepareArbitraryStateD(
tolerance : Double,
coefficients : ComplexPolar[],
coefficients : Double[],
qubits : LittleEndian
)
: Unit is Adj + Ctl {
(_CompileApproximateArbitraryStatePreparation(tolerance, coefficients, Length(qubits!)))(qubits);
let coefficientsAsComplexPolar = Mapped(Compose(ComplexPolar(_, 0.0), AbsD), coefficients);
ApproximatelyPrepareArbitraryStateCP(tolerance, coefficientsAsComplexPolar, qubits);
}

/// # Summary
Expand Down Expand Up @@ -353,4 +364,3 @@ namespace Microsoft.Quantum.Preparation {

}


Loading