Skip to content

Commit

Permalink
Updated the last set of stylecop issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
RolfHuisman committed Jul 25, 2020
1 parent 6c069fd commit b397a04
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/CHPSimulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<None Remove="stylecop.json" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Simulators" Version="0.12.20072031" />
<PackageReference Include="Microsoft.Quantum.IQSharp.Jupyter" Version="0.12.20072031" />
Expand All @@ -19,6 +27,7 @@
<PackageLicenceExpression>MIT</PackageLicenceExpression>
<RepositoryUrl>https://github.com/qsharp-community/chp-sim.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<DocumentationFile>CHPSimulator.xml</DocumentationFile>
</PropertyGroup>


Expand Down
399 changes: 399 additions & 0 deletions src/CHPSimulator.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/DebugMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <copyright file="DebugMessage.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// <copyright file="DebugMessage.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
#nullable enable

Expand Down
96 changes: 93 additions & 3 deletions src/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="Extensions.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>

// This C# project is based on a Python implementation by @Strilanc here:
Expand All @@ -17,6 +16,12 @@ namespace QSharpCommunity.Simulators.Chp
/// </summary>
internal static class Extensions
{
/// <summary>
/// Set the diagonal line of the matix to the value.
/// </summary>
/// <typeparam name="T">Matrix type.</typeparam>
/// <param name="matrix">The matrix.</param>
/// <param name="value">The value to set it at.</param>
internal static void SetDiagonal<T>(this T[,] matrix, T value)
{
// TODO: better safety checking for the num rows
Expand All @@ -26,6 +31,12 @@ internal static void SetDiagonal<T>(this T[,] matrix, T value)
}
}

/// <summary>
/// Swapped the collumns.
/// </summary>
/// <param name="matrix">The matrix.</param>
/// <param name="idx1">Collum to swap with idx2.</param>
/// <param name="idx2">Collum to swap with idx1.</param>
internal static void SwapColumns(this bool[,] matrix, int idx1, int idx2)
{
foreach (var idxRow in Enumerable.Range(0, matrix.GetLength(0)))
Expand All @@ -36,6 +47,13 @@ internal static void SwapColumns(this bool[,] matrix, int idx1, int idx2)
}
}

/// <summary>
/// Returns the row of the matrix.
/// </summary>
/// <typeparam name="T">the matrix type.</typeparam>
/// <param name="matrix">The matrix.</param>
/// <param name="idxRow">The row that needs to be returned.</param>
/// <returns>The row highlighted by idxRow.</returns>
internal static IEnumerable<T> Row<T>(this T[,] matrix, int idxRow)
{
foreach (var idxColumn in Enumerable.Range(0, matrix.GetLength(1)))
Expand All @@ -44,6 +62,13 @@ internal static IEnumerable<T> Row<T>(this T[,] matrix, int idxRow)
}
}

/// <summary>
/// Returns the column of the matrix.
/// </summary>
/// <typeparam name="T">the matrix type.</typeparam>
/// <param name="matrix">The matrix.</param>
/// <param name="idxColumn">The column that needs to be returned.</param>
/// <returns>The row highlighted by idxColumn.</returns>
internal static IEnumerable<T> Column<T>(this T[,] matrix, int idxColumn)
{
foreach (var idxRow in Enumerable.Range(0, matrix.GetLength(0)))
Expand All @@ -52,6 +77,11 @@ internal static IEnumerable<T> Column<T>(this T[,] matrix, int idxColumn)
}
}

/// <summary>
/// Represents the Row including the phase.
/// </summary>
/// <param name="vector">Row represented as a vector.</param>
/// <returns>The rendered row including the phase.</returns>
internal static string RowToString(this bool[] vector)
{
var (xs, zs, r) = vector.SplitRow();
Expand All @@ -67,17 +97,41 @@ internal static string RowToString(this bool[] vector)
}));
}

/// <summary>
/// Renders the row to a text string.
/// </summary>
/// <param name="matrix">The Matrix.</param>
/// <param name="idx">Row index.</param>
/// <returns>A rendered string of the row.</returns>
internal static string RowToString(this bool[,] matrix, int idx) => matrix.Row(idx).ToArray().RowToString();

/// <summary>
/// Renders the row to a latex usable text string.
/// </summary>
/// <param name="matrix">The Matrix.</param>
/// <param name="idx">Row index.</param>
/// <returns>A rendered string of the row.</returns>
internal static string RowToLatex(this bool[,] matrix, int idx) =>
string.Join(" & ", matrix.Row(idx).ToArray().Select(val => val ? 1 : 0));

/// <summary>
/// Renders the matrix to a text string.
/// </summary>
/// <param name="matrix">The Matrix.</param>
/// <param name="showDestabilizers">Include the stabilizers in the string.</param>
/// <returns>A rendered string of the table.</returns>
internal static string MatrixToString(this bool[,] matrix, bool showDestabilizers = false) =>
"<" + string.Join(", ", Enumerable.Range(matrix.GetLength(0) / 2, matrix.GetLength(0) / 2).Select(idx => matrix.RowToString(idx))) + ">" +
(showDestabilizers ?
"| >" + string.Join(", ", Enumerable.Range(0, matrix.GetLength(0) / 2).Select(idx => matrix.RowToString(idx))) + "<" :
">");

/// <summary>
/// Renders the matrixto a latex usable text string.
/// </summary>
/// <param name="matrix">The Matrix.</param>
/// <param name="showDestabilizers">Include the stabilizers in the string.</param>
/// <returns>A rendered string of the table.</returns>
internal static string MatrixToLatexString(this bool[,] matrix, bool showDestabilizers = false) =>
(
showDestabilizers
Expand All @@ -86,13 +140,24 @@ internal static string MatrixToLatexString(this bool[,] matrix, bool showDestabi
: string.Empty) +
string.Join(@" \\", Enumerable.Range(matrix.GetLength(0) / 2, matrix.GetLength(0) / 2).Select(idx => matrix.RowToLatex(idx)));

internal static (bool[] Stabilzers, bool[] DeStabilzers, bool Phase) SplitRow(this IEnumerable<bool> row)
/// <summary>
/// Split the row into; stabalizers, destabilzers, and phase.
/// </summary>
/// <param name="row">Boolean representation of the row.</param>
/// <returns>3-Element Tuple with seperated stabalizers, destabilzers, and phase.</returns>
internal static (bool[] Stabalizers, bool[] DeStabilzers, bool Phase) SplitRow(this IEnumerable<bool> row)
{
var vector = row.ToArray();
var nQubits = (vector.Length - 1) / 2;
return (vector[0..nQubits], vector[nQubits..^1], vector[^1]);
}

/// <summary>
/// Calculate the product of the phase.
/// </summary>
/// <param name="row1">Left vector.</param>
/// <param name="row2">Right vector.</param>
/// <returns>PhaseProduct.</returns>
internal static bool PhaseProduct(this IEnumerable<bool> row1, IEnumerable<bool> row2)
{
static int G(bool x1, bool z1, bool x2, bool z2) =>
Expand All @@ -116,6 +181,12 @@ static int G(bool x1, bool z1, bool x2, bool z2) =>
return ((r1 ? 2 : 0) + (r2 ? 2 : 0) + acc) % 4 == 2;
}

/// <summary>
/// Set the row sum for the Matrix.
/// </summary>
/// <param name="matrix">The Matrix.</param>
/// <param name="idxTarget">Index of vector to.</param>
/// <param name="idxSource">Index of vector from.</param>
internal static void SetToRowSum(this bool[,] matrix, int idxTarget, int idxSource)
{
foreach (var idxColumn in Enumerable.Range(0, matrix.GetLength(1) - 1))
Expand All @@ -126,6 +197,12 @@ internal static void SetToRowSum(this bool[,] matrix, int idxTarget, int idxSour
matrix[idxTarget, matrix.GetLength(1) - 1] = matrix.Row(idxTarget).PhaseProduct(matrix.Row(idxSource));
}

/// <summary>
/// Set the row sum for the Matrix.
/// </summary>
/// <param name="vector">The vector.</param>
/// <param name="matrix">The Matrix.</param>
/// <param name="idxSource">Index of vector from.</param>
internal static void SetToRowSum(this bool[] vector, bool[,] matrix, int idxSource)
{
foreach (var idxColumn in Enumerable.Range(0, matrix.GetLength(1) - 1))
Expand All @@ -136,6 +213,13 @@ internal static void SetToRowSum(this bool[] vector, bool[,] matrix, int idxSour
vector[matrix.GetLength(1) - 1] = vector.PhaseProduct(matrix.Row(idxSource));
}

/// <summary>
/// Search the matrix.
/// </summary>
/// <typeparam name="T">Type of the matrix.</typeparam>
/// <param name="source">Source list to check.</param>
/// <param name="predicate">Predicate to check.</param>
/// <returns>Indices of where predicate is true.</returns>
internal static IEnumerable<int> IndicesWhere<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (var item in source.Select((element, idx) => (element, idx)))
Expand All @@ -147,6 +231,12 @@ internal static IEnumerable<int> IndicesWhere<T>(this IEnumerable<T> source, Fun
}
}

/// <summary>
/// Try to find the PauliZ that it can be measured on (if there isn't a measurement on X or Y.).
/// </summary>
/// <param name="paulis">The Pauli axis being measured on.</param>
/// <param name="idx">The index of the PauliX. -1 otherwise.</param>
/// <returns>If it can be measured on a single PauliZ.</returns>
internal static bool TryGetSingleZ(this IEnumerable<Pauli> paulis, out int idx)
{
if (paulis.Any(basis => basis == Pauli.PauliX || basis == Pauli.PauliY))
Expand Down
4 changes: 2 additions & 2 deletions src/StabilizerProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="StabilizerProcessor.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
#nullable enable

Expand Down Expand Up @@ -322,6 +321,7 @@ public override void AssertProb(IQArray<Pauli> bases, IQArray<Qubit> qubits, dou
}
}

/// <inheritdoc/>
public override Result M(Qubit qubit) => this.MeasureByIndex(qubit.Id);

/// <inheritdoc/>
Expand Down
14 changes: 12 additions & 2 deletions src/StabilizerSimulator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="StabilizerSimulator.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
// This C# project is based on a Python implementation by @Strilanc here:
// https://github.com/Strilanc/python-chp-stabilizer-simulator
Expand All @@ -11,14 +10,25 @@ namespace QSharpCommunity.Simulators.Chp
using Microsoft.Quantum.Simulation.Common;
using Microsoft.Quantum.Simulation.QuantumProcessor;

/// <summary>
/// CHP Simulator.
/// </summary>
public class StabilizerSimulator : QuantumProcessorDispatcher
{
/// <summary>
/// Initializes a new instance of the <see cref="StabilizerSimulator"/> class.
/// </summary>
/// <param name="nQubits">Qubits to use for simulation.</param>
public StabilizerSimulator(int? nQubits = null)
: base(new StabilizerProcessor(nQubits))
{
(this.QuantumProcessor as StabilizerProcessor).Simulator = this;
}

/// <summary>
/// If diagnostic is enabled, show diagnostic information.
/// </summary>
/// <param name="displayable">Diagnostic displayable information.</param>
internal new void MaybeDisplayDiagnostic(object displayable) =>
base.MaybeDisplayDiagnostic(displayable);
}
Expand Down
3 changes: 1 addition & 2 deletions src/jupyter/ChpMagic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="ChpMagic.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
// Adapted from Toffoli magic command in the IQSharp project here:
// https://github.com/microsoft/iqsharp/blob/master/src/Kernel/Magic/ToffoliMagic.cs
Expand Down
2 changes: 1 addition & 1 deletion src/jupyter/StabilizerTableau.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <copyright file="StabilizerTableau.cs" company="https://qsharp.community/">
// Copyright (c) PlaceholderCompany. All rights reserved.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
// Adapted from State display encoders in the IQSharp project here:
// https://github.com/microsoft/iqsharp/blob/master/src/Jupyter/Visualization/StateDisplayEncoders.cs
Expand Down
17 changes: 14 additions & 3 deletions src/jupyter/TableauToHtmlEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="TableauToHtmlEncoder.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
// Adapted from State display encoders in the IQSharp project here:
// https://github.com/microsoft/iqsharp/blob/master/src/Jupyter/Visualization/StateDisplayEncoders.cs
Expand All @@ -13,19 +12,31 @@ namespace QSharpCommunity.Simulators.Chp
using Microsoft.Quantum.IQSharp.Jupyter;

/// <summary>
/// Tableau visualizer for jupiter.
/// Encodes the Tableau in HTML so it can be rendered by jupiter.
/// </summary>
public class TableauToHtmlEncoder : IResultEncoder
{
private readonly IConfigurationSource configurationSource;

/// <summary>
/// Initializes a new instance of the <see cref="TableauToHtmlEncoder"/> class.
/// </summary>
/// <param name="configurationSource">Settings to be used.</param>
public TableauToHtmlEncoder(IConfigurationSource configurationSource)
{
this.configurationSource = configurationSource;
}

/// <summary>
/// Gets the mimetype used for rendering that its html.
/// </summary>
public string MimeType => MimeTypes.Html;

/// <summary>
/// Returns the StabilizerTableau into the text as encoded data.
/// </summary>
/// <param name="displayable">Should be the StabilizerTableau.</param>
/// <returns>Text encoded table.</returns>
public EncodedData? Encode(object displayable)
{
if (displayable is StabilizerTableau tableau)
Expand Down
14 changes: 12 additions & 2 deletions src/jupyter/TableauToTextEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// <copyright file="TableauToTextEncoder.cs" company="https://qsharp.community/">
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
// Copyright (c) Sarah Kaiser. Licensed under the MIT License.
// </copyright>
// Adapted from State display encoders in the IQSharp project here:
// https://github.com/microsoft/iqsharp/blob/master/src/Jupyter/Visualization/StateDisplayEncoders.cs
Expand All @@ -12,10 +11,21 @@ namespace QSharpCommunity.Simulators.Chp
using Microsoft.Jupyter.Core;
using Microsoft.Quantum.IQSharp.Jupyter;

/// <summary>
/// Encodes the Tableau in text so it can be rendered by jupiter.
/// </summary>
public class TableauToTextEncoder : IResultEncoder
{
/// <summary>
/// Gets the mimetype used for rendering that its text.
/// </summary>
public string MimeType => MimeTypes.PlainText;

/// <summary>
/// Returns the StabilizerTableau into the text as encoded data.
/// </summary>
/// <param name="displayable">Should be the StabilizerTableau.</param>
/// <returns>Text encoded table.</returns>
public EncodedData? Encode(object displayable)
{
if (displayable is StabilizerTableau tableau)
Expand Down
2 changes: 1 addition & 1 deletion src/stylecop.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"settings": {
"documentationRules": {
"companyName": "https://qsharp.community/",
"copyrightText": "Copyright (c) Sarah Kaiser. All rights reserved."
"copyrightText": "Copyright (c) Sarah Kaiser. Licensed under the MIT License."
}
}
}

0 comments on commit b397a04

Please sign in to comment.