Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Enabled the clang-format check (#794)
Browse files Browse the repository at this point in the history
* Added `check-sources-formatted.ps1`
* Enabled the format checking.
* Build QIR RT first.
* Renamed src/Qir/Common/externals to src/Qir/Common/Externals
* Excluded FullStateDriverGenerator.
* Excluded Mac from clang-formatting.
  • Loading branch information
kuzminrobin authored Aug 18, 2021
1 parent b57d34d commit 9f35c0b
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 115 deletions.
16 changes: 8 additions & 8 deletions build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ $ErrorActionPreference = 'Stop'
& "$PSScriptRoot/set-env.ps1"
$all_ok = $True

if ($Env:ENABLE_NATIVE -ne "false") {
$nativeSimulator = (Join-Path $PSScriptRoot "../src/Simulation/Native")
& "$nativeSimulator/build-native-simulator.ps1"
if ($Env:ENABLE_QIRRUNTIME -ne "false") {
$qirRuntime = (Join-Path $PSScriptRoot "../src/Qir/Runtime")
& "$qirRuntime/build-qir-runtime.ps1"
if ($LastExitCode -ne 0) {
$script:all_ok = $False
}
} else {
Write-Host "Skipping build of native simulator because ENABLE_NATIVE variable is set to: $Env:ENABLE_NATIVE."
Write-Host "Skipping build of qir runtime because ENABLE_QIRRUNTIME variable is set to: $Env:ENABLE_QIRRUNTIME"
}

if ($Env:ENABLE_QIRRUNTIME -ne "false") {
$qirRuntime = (Join-Path $PSScriptRoot "../src/Qir/Runtime")
& "$qirRuntime/build-qir-runtime.ps1"
if ($Env:ENABLE_NATIVE -ne "false") {
$nativeSimulator = (Join-Path $PSScriptRoot "../src/Simulation/Native")
& "$nativeSimulator/build-native-simulator.ps1"
if ($LastExitCode -ne 0) {
$script:all_ok = $False
}
} else {
Write-Host "Skipping build of qir runtime because ENABLE_QIRRUNTIME variable is set to: $Env:ENABLE_QIRRUNTIME"
Write-Host "Skipping build of native simulator because ENABLE_NATIVE variable is set to: $Env:ENABLE_NATIVE."
}

if ($Env:ENABLE_EXPERIMENTALSIM -ne "false") {
Expand Down
8 changes: 4 additions & 4 deletions build/prerequisites.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
$ErrorActionPreference = 'Stop'
& "$PSScriptRoot/set-env.ps1"

Push-Location (Join-Path $PSScriptRoot "../src/Qir/Runtime")
.\prerequisites.ps1
Pop-Location

Push-Location (Join-Path $PSScriptRoot "../src/Simulation/Simulators")
.\FindNuspecReferences.ps1
Pop-Location
Expand All @@ -17,10 +21,6 @@ if ($Env:ENABLE_NATIVE -ne "false") {
Write-Host "Skipping installing prerequisites for native simulator because ENABLE_NATIVE variable set to: $Env:ENABLE_NATIVE"
}

Push-Location (Join-Path $PSScriptRoot "../src/Qir/Runtime")
.\prerequisites.ps1
Pop-Location

if ($Env:ENABLE_EXPERIMENTALSIM -ne "false") {
Push-Location (Join-Path $PSScriptRoot "../src/Simulation/qdk_sim_rs")
.\prerequisites.ps1
Expand Down
5 changes: 4 additions & 1 deletion build/set-env.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ Get-ChildItem @(
"Env:\CRATE_OUTDIR",
"Env:\WHEEL_OUTDIR",
"Env:\DOCS_OUTDIR"
) | Format-Table
) | Format-Table

Write-Host "PATH:"
Write-Host "$Env:PATH"
File renamed without changes.
7 changes: 4 additions & 3 deletions src/Qir/Common/Include/FloatUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@

#include <type_traits>

// Comparing floating point values with `==` or `!=` is not reliable.
// Comparing floating point values with `==` or `!=` is not reliable.
// The values can be extremely close but still not exactly equal.
// It is more reliable to check if one value is within certain small tolerance near the other value.
// This template function is for comparing two floating point values.
template<typename TFloat>
inline bool Close(TFloat val1, TFloat val2)
{
assert(std::is_floating_point_v< std::remove_reference_t<TFloat> > && "Unexpected type is passed as a template argument");
assert(std::is_floating_point_v<std::remove_reference_t<TFloat>> &&
"Unexpected type is passed as a template argument");

constexpr TFloat tolerance = 1e-10;

// Both val1 and val2 can be close (or equal) to the maximum (or minimum) value representable with its type.
// Adding to (or subtracting from) such a value can cause overflow (or underflow).
// To avoid the overflow (or underflow) we don't add to the greater value (and don't sutract from a lesser value).
if(val1 < val2)
if (val1 < val2)
{
return (val2 - val1) <= tolerance;
}
Expand Down
16 changes: 7 additions & 9 deletions src/Qir/Common/Include/SimulatorStub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Microsoft
{
namespace Quantum
{
struct SimulatorStub : public IRuntimeDriver, public IQuantumGateSet
struct SimulatorStub
: public IRuntimeDriver
, public IQuantumGateSet
{
Qubit AllocateQubit() override
{
Expand Down Expand Up @@ -79,17 +81,13 @@ namespace Quantum
{
throw std::logic_error("not_implemented: ControlledT");
}
void ControlledR(long /*numControls*/, Qubit* /*controls*/, PauliId /*axis*/, Qubit /*target*/, double /*theta*/) override
void ControlledR(long /*numControls*/, Qubit* /*controls*/, PauliId /*axis*/, Qubit /*target*/,
double /*theta*/) override
{
throw std::logic_error("not_implemented: ControlledR");
}
void ControlledExp(
long /*numControls*/,
Qubit* /*controls*/,
long /*numTargets*/,
PauliId* /*paulis*/,
Qubit* /*targets*/,
double /*theta*/) override
void ControlledExp(long /*numControls*/, Qubit* /*controls*/, long /*numTargets*/, PauliId* /*paulis*/,
Qubit* /*targets*/, double /*theta*/) override
{
throw std::logic_error("not_implemented: ControlledExp");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The QIR runtime includes an implementation of the
- `public` folder contains the public headers
- `lib` folder contains the implementation of the runtime and the simulators.
- `unittests` folder contains tests for the runtime
- `externals` folder contains external dependencies. We'll strive to keep those minimal.
- `Externals` folder contains external dependencies. We'll strive to keep those minimal.

## Build

Expand Down
5 changes: 5 additions & 0 deletions src/Qir/Runtime/build-qir-runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

. (Join-Path $PSScriptRoot .. qir-utils.ps1)

Write-Host "##[info]Compile QIR Runtime"

& (Join-Path $PSScriptRoot ".." check-sources-formatted.ps1) -Path $PSScriptRoot
& (Join-Path $PSScriptRoot ".." check-sources-formatted.ps1) -Path (Join-Path $PSScriptRoot ".." Common)

if (-not (Build-CMakeProject $PSScriptRoot "QIR Runtime")) {
throw "At least one project failed to compile. Check the logs."
}
Expand Down
10 changes: 9 additions & 1 deletion src/Qir/Runtime/prerequisites.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

#Requires -Version 6.0

if ($Env:ENABLE_QIRRUNTIME -ne "false") {
if (($IsWindows) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win")))) {
if (!(Get-Command clang -ErrorAction SilentlyContinue)) {
if (!(Get-Command clang -ErrorAction SilentlyContinue) -or `
!(Get-Command clang-format -ErrorAction SilentlyContinue)) {
choco install llvm --version=11.1.0
Write-Host "##vso[task.setvariable variable=PATH;]$Env:Path;C:\Program Files\LLVM\bin"
}
if (!(Get-Command ninja -ErrorAction SilentlyContinue)) {
choco install ninja
}
if (!(Get-Command cmake -ErrorAction SilentlyContinue)) {
choco install cmake
}
refreshenv
} elseif ($IsMacOS) {
# temporary workaround for Bintray sunset
# remove this after Homebrew is updated to 3.1.1 on MacOS image, see:
# https://github.com/actions/virtual-environments/blob/main/images/macos/macos-10.15-Readme.md
brew update
brew install ninja
if (!(Get-Command clang-format -ErrorAction SilentlyContinue)) {
brew install clang-format
}
} else {
if (Get-Command sudo -ErrorAction SilentlyContinue) {
sudo apt update
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Runtime/public/CoreTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef RESULT* Result; // TODO: Replace with `typedef uintXX_t Result`, where X
enum ResultValue
{
Result_Zero = 0,
Result_One = 1,
Result_One = 1,
Result_Pending, // indicates that this is a deferred result
};

Expand Down
79 changes: 36 additions & 43 deletions src/Qir/Runtime/public/QSharpSimApi_I.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,35 @@ namespace Quantum
{
struct QIR_SHARED_API IQuantumGateSet
{
virtual ~IQuantumGateSet() {}
virtual ~IQuantumGateSet()
{
}
IQuantumGateSet() = default;

// Elementary operatons
virtual void X(Qubit target) = 0;
virtual void Y(Qubit target) = 0;
virtual void Z(Qubit target) = 0;
virtual void H(Qubit target) = 0;
virtual void S(Qubit target) = 0;
virtual void T(Qubit target) = 0;
virtual void R(PauliId axis, Qubit target, double theta) = 0;
virtual void X(Qubit target) = 0;
virtual void Y(Qubit target) = 0;
virtual void Z(Qubit target) = 0;
virtual void H(Qubit target) = 0;
virtual void S(Qubit target) = 0;
virtual void T(Qubit target) = 0;
virtual void R(PauliId axis, Qubit target, double theta) = 0;
virtual void Exp(long numTargets, PauliId paulis[], Qubit targets[], double theta) = 0;

// Multicontrolled operations
virtual void ControlledX(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledY(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledZ(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledH(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledS(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledT(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledX(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledY(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledZ(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledH(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledS(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledT(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledR(long numControls, Qubit controls[], PauliId axis, Qubit target, double theta) = 0;
virtual void ControlledExp(
long numControls,
Qubit controls[],
long numTargets,
PauliId paulis[],
Qubit targets[],
double theta) = 0;
virtual void ControlledExp(long numControls, Qubit controls[], long numTargets, PauliId paulis[],
Qubit targets[], double theta) = 0;

// Adjoint operations
virtual void AdjointS(Qubit target) = 0;
virtual void AdjointT(Qubit target) = 0;
virtual void AdjointS(Qubit target) = 0;
virtual void AdjointT(Qubit target) = 0;
virtual void ControlledAdjointS(long numControls, Qubit controls[], Qubit target) = 0;
virtual void ControlledAdjointT(long numControls, Qubit controls[], Qubit target) = 0;

Expand All @@ -54,12 +51,14 @@ namespace Quantum

private:
IQuantumGateSet& operator=(const IQuantumGateSet&) = delete;
IQuantumGateSet(const IQuantumGateSet&) = delete;
IQuantumGateSet(const IQuantumGateSet&) = delete;
};

struct QIR_SHARED_API IDiagnostics
{
virtual ~IDiagnostics() {}
virtual ~IDiagnostics()
{
}
IDiagnostics() = default;

// The callback should be invoked on each basis vector (in the standard computational basis) in little-endian
Expand All @@ -69,29 +68,23 @@ namespace Quantum
// Deprecated, use `DumpMachine()` and `DumpRegister()` instead.
virtual void GetState(TGetStateCallback callback) = 0;

virtual void DumpMachine(const void* location) = 0;
virtual void DumpMachine(const void* location) = 0;
virtual void DumpRegister(const void* location, const QirArray* qubits) = 0;

// Both Assert methods return `true`, if the assert holds, `false` otherwise.
virtual bool Assert(
long numTargets,
PauliId bases[],
Qubit targets[],
Result result,
const char* failureMessage) = 0; // TODO: The `failureMessage` is not used, consider removing. The `bool` is returned.

virtual bool AssertProbability(
long numTargets,
PauliId bases[],
Qubit targets[],
double probabilityOfZero,
double precision,
const char* failureMessage) = 0; // TODO: The `failureMessage` is not used, consider removing. The `bool` is returned.
virtual bool Assert(long numTargets, PauliId bases[], Qubit targets[], Result result,
const char* failureMessage) = 0; // TODO: The `failureMessage` is not used, consider
// removing. The `bool` is returned.

virtual bool AssertProbability(long numTargets, PauliId bases[], Qubit targets[], double probabilityOfZero,
double precision,
const char* failureMessage) = 0; // TODO: The `failureMessage` is not used,
// consider removing. The `bool` is returned.

private:
IDiagnostics& operator=(const IDiagnostics&) = delete;
IDiagnostics(const IDiagnostics&) = delete;
IDiagnostics(const IDiagnostics&) = delete;
};

}
}
} // namespace Quantum
} // namespace Microsoft
14 changes: 8 additions & 6 deletions src/Qir/Runtime/public/QirRuntimeApi_I.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ namespace Quantum
{
struct QIR_SHARED_API IRuntimeDriver
{
virtual ~IRuntimeDriver() {}
virtual ~IRuntimeDriver()
{
}
IRuntimeDriver() = default;

// Doesn't necessarily provide insight into the state of the qubit (for that look at IDiagnostics)
virtual std::string QubitToString(Qubit qubit) = 0;

// Qubit management
virtual Qubit AllocateQubit() = 0;
virtual Qubit AllocateQubit() = 0;
virtual void ReleaseQubit(Qubit qubit) = 0;

virtual void ReleaseResult(Result result) = 0;
virtual void ReleaseResult(Result result) = 0;
virtual bool AreEqualResults(Result r1, Result r2) = 0;
virtual ResultValue GetResultValue(Result result) = 0;
virtual ResultValue GetResultValue(Result result) = 0;
// The caller *should not* release results obtained via these two methods. The
// results are guaranteed to be finalized to the corresponding ResultValue, but
// it's not required from the runtime to return same Result on subsequent calls.
virtual Result UseZero() = 0;
virtual Result UseOne() = 0;
virtual Result UseOne() = 0;

private:
IRuntimeDriver& operator=(const IRuntimeDriver&) = delete;
IRuntimeDriver(const IRuntimeDriver&) = delete;
IRuntimeDriver(const IRuntimeDriver&) = delete;
};

} // namespace Quantum
Expand Down
Loading

0 comments on commit 9f35c0b

Please sign in to comment.