Skip to content

Commit

Permalink
apply clang-format only src/simulators/*stabilizer
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Mar 2, 2023
1 parent c4f7578 commit 64311f4
Show file tree
Hide file tree
Showing 9 changed files with 2,284 additions and 2,807 deletions.
684 changes: 290 additions & 394 deletions src/simulators/extended_stabilizer/ch_runner.hpp

Large diffs are not rendered by default.

1,449 changes: 649 additions & 800 deletions src/simulators/extended_stabilizer/chlib/chstabilizer.hpp

Large diffs are not rendered by default.

490 changes: 198 additions & 292 deletions src/simulators/extended_stabilizer/chlib/core.hpp

Large diffs are not rendered by default.

1,017 changes: 464 additions & 553 deletions src/simulators/extended_stabilizer/extended_stabilizer_state.hpp

Large diffs are not rendered by default.

265 changes: 117 additions & 148 deletions src/simulators/extended_stabilizer/gates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,35 @@
#include "framework/operations.hpp"
#include "framework/types.hpp"

namespace CHSimulator
{
using uint_t = uint_fast64_t;
using complex_t = std::complex<double>;

enum class Gates {
u0, u1, id, x, y, z, h, s, sdg, sx, sxdg, t, tdg,
cx, cz, swap,
ccx, ccz, pauli
};

enum class Gatetypes {
pauli,
clifford,
non_clifford
};

const AER::stringmap_t<Gatetypes> gate_types_ = {
namespace CHSimulator {
using uint_t = uint_fast64_t;
using complex_t = std::complex<double>;

enum class Gates {
u0,
u1,
id,
x,
y,
z,
h,
s,
sdg,
sx,
sxdg,
t,
tdg,
cx,
cz,
swap,
ccx,
ccz,
pauli
};

enum class Gatetypes { pauli, clifford, non_clifford };

const AER::stringmap_t<Gatetypes> gate_types_ = {
// One-qubit gates
{"u0", Gatetypes::pauli}, // Pauli-Identity gate
{"delay", Gatetypes::pauli}, // Pauli-Identity gate
Expand All @@ -59,51 +70,50 @@ namespace CHSimulator
{"u1", Gatetypes::non_clifford}, // zero-X90 pulse waltz gate
{"p", Gatetypes::non_clifford}, // zero-X90 pulse waltz gate
// Two-qubit gates
{"CX", Gatetypes::clifford}, // Controlled-X gate (CNOT)
{"cx", Gatetypes::clifford}, // Controlled-X gate (CNOT)
{"cz", Gatetypes::clifford}, // Controlled-Z gate
{"swap", Gatetypes::clifford}, // SWAP gate
{"CX", Gatetypes::clifford}, // Controlled-X gate (CNOT)
{"cx", Gatetypes::clifford}, // Controlled-X gate (CNOT)
{"cz", Gatetypes::clifford}, // Controlled-Z gate
{"swap", Gatetypes::clifford}, // SWAP gate
// Three-qubit gates
{"ccx", Gatetypes::non_clifford}, // Controlled-CX gate (Toffoli)
{"ccz", Gatetypes::non_clifford}, // Controlled-CZ gate (H3 Toff H3)
// N-qubit gates
{"pauli", Gatetypes::pauli}, // N-qubit Pauli gate
};

using sample_branch_t = std::pair<complex_t, Gates>;

const double root2 = std::sqrt(2);
const double root1_2 = 1./root2;
const complex_t pi_over_8_phase(0., M_PI/8);
const complex_t omega(root1_2, root1_2);
const complex_t omega_star(root1_2, -1*root1_2);
const complex_t root_omega = std::exp(pi_over_8_phase);
const complex_t root_omega_star = std::conj(root_omega);

const double tan_pi_over_8 = std::tan(M_PI/8.);

//Base class for sampling over non-Clifford gates in the Sum Over Cliffords routine.
struct Sample {
public:
Sample() = default;
virtual ~Sample() = default;
Sample(const Sample& other) : branches(other.branches) {};
std::vector<sample_branch_t> branches;

virtual sample_branch_t sample(double r) const = 0;
};

//Functor class that defines how to sample branches over a U1 operation
//Used for caching each U1 gate angle we encounter in the circuit
struct U1Sample : public Sample
{
{"pauli", Gatetypes::pauli}, // N-qubit Pauli gate
};

using sample_branch_t = std::pair<complex_t, Gates>;

const double root2 = std::sqrt(2);
const double root1_2 = 1. / root2;
const complex_t pi_over_8_phase(0., M_PI / 8);
const complex_t omega(root1_2, root1_2);
const complex_t omega_star(root1_2, -1 * root1_2);
const complex_t root_omega = std::exp(pi_over_8_phase);
const complex_t root_omega_star = std::conj(root_omega);

const double tan_pi_over_8 = std::tan(M_PI / 8.);

// Base class for sampling over non-Clifford gates in the Sum Over Cliffords
// routine.
struct Sample {
public:
Sample() = default;
virtual ~Sample() = default;
Sample(const Sample &other) : branches(other.branches){};
std::vector<sample_branch_t> branches;

virtual sample_branch_t sample(double r) const = 0;
};

// Functor class that defines how to sample branches over a U1 operation
// Used for caching each U1 gate angle we encounter in the circuit
struct U1Sample : public Sample {
double p_threshold;

U1Sample() = default;
U1Sample(double lambda);

U1Sample(const U1Sample &other) : Sample(other)
{
U1Sample(const U1Sample &other) : Sample(other) {
p_threshold = other.p_threshold;
}

Expand All @@ -112,135 +122,94 @@ struct U1Sample : public Sample
sample_branch_t sample(double r) const override;
};

U1Sample::U1Sample(double lambda)
{
U1Sample::U1Sample(double lambda) {
// Shift parameter into +- 2 Pi
uint_t shift_factor = std::floor(std::abs(lambda)/(2*M_PI));
if (shift_factor != 0)
{
if(lambda < 0)
{
lambda += shift_factor*(2*M_PI);
}
uint_t shift_factor = std::floor(std::abs(lambda) / (2 * M_PI));
if (shift_factor != 0) {
if (lambda < 0)
lambda += shift_factor * (2 * M_PI);
else
{
lambda -= shift_factor*(2*M_PI);
}
lambda -= shift_factor * (2 * M_PI);
}
// Shift parameter into +- Pi
if (lambda > M_PI)
{
lambda -= 2*M_PI;
}
else if (lambda < -1*M_PI)
{
lambda += 2*M_PI;
}
lambda -= 2 * M_PI;
else if (lambda < -1 * M_PI)
lambda += 2 * M_PI;
// Compute the coefficients
double angle = std::abs(lambda);
bool s_z_quadrant = (angle > M_PI/2);
if(s_z_quadrant)
{
angle = angle - M_PI/2;
}
bool s_z_quadrant = (angle > M_PI / 2);
if (s_z_quadrant)
angle = angle - M_PI / 2;
angle /= 2;
complex_t coeff_0 = std::cos(angle)-std::sin(angle);
complex_t coeff_1 = root2*std::sin(angle);
complex_t coeff_0 = std::cos(angle) - std::sin(angle);
complex_t coeff_1 = root2 * std::sin(angle);
complex_t phase_0, phase_1;
std::array<Gates, 2> gates;
if(lambda < 0)
{
if (lambda < 0) {
coeff_0 *= root_omega_star;
coeff_1 = coeff_1 * root_omega;
if(s_z_quadrant)
{
if (s_z_quadrant) {
gates[0] = Gates::sdg;
gates[1] = Gates::z;
}
else
{
} else {
gates[0] = Gates::id;
gates[1] = Gates::sdg;
}
}
else
{
} else {
coeff_0 *= root_omega;
coeff_1 = coeff_1 * root_omega_star;
if(s_z_quadrant)
{
if (s_z_quadrant) {
gates[0] = Gates::s;
gates[1] = Gates::z;
}
else
{
} else {
gates[0] = Gates::id;
gates[1] = Gates::s;
}
}
phase_0 = std::polar(1.0, std::arg(coeff_0));
phase_1 = std::polar(1.0, std::arg(coeff_1));
branches =
{
sample_branch_t(phase_0, gates[0]),
sample_branch_t(phase_1, gates[1])
};
p_threshold = std::abs(coeff_0) / (std::abs(coeff_0)+std::abs(coeff_1));
branches = {sample_branch_t(phase_0, gates[0]),
sample_branch_t(phase_1, gates[1])};
p_threshold = std::abs(coeff_0) / (std::abs(coeff_0) + std::abs(coeff_1));
}

sample_branch_t U1Sample::sample(double r) const
{
if (r<p_threshold)
{
sample_branch_t U1Sample::sample(double r) const {
if (r < p_threshold)
return branches[0];
}
else
{
return branches[1];
}
}

// Stabilizer extent for different non-Clifford unitaries
// Special case of Eq. 28 in arXiv:1808.00128
const double t_extent = std::pow(1./(std::cos(M_PI/8.)), 2);
// Equation 32 in arXiv: 1808.00128
const double ccx_extent = 16./9.;
const double ccx_coeff = 1./6.;
//General result for z rotations, Eq. 28 in arXiv 1809.00128
inline double u1_extent(double lambda)
{
// Shift parameter into +- 2 Pi
uint_t shift_factor = std::floor(std::abs(lambda)/(2*M_PI));
if (shift_factor != 0)
{
if(lambda < 0)
{
lambda += shift_factor*(2*M_PI);
}
else
{
lambda -= shift_factor*(2*M_PI);
}
}
// Shift parameter into +- Pi
if (lambda > M_PI)
{
lambda -= 2*M_PI;
}
else if (lambda < -1*M_PI)
{
lambda += 2*M_PI;
}
double angle = std::abs(lambda);
bool s_z_quadrant = (angle > M_PI/2);
if(s_z_quadrant)
{
angle = angle - M_PI/2;
}
angle /= 2;
return std::pow(std::cos(angle) + tan_pi_over_8*std::sin(angle), 2.);
// Stabilizer extent for different non-Clifford unitaries
// Special case of Eq. 28 in arXiv:1808.00128
const double t_extent = std::pow(1. / (std::cos(M_PI / 8.)), 2);
// Equation 32 in arXiv: 1808.00128
const double ccx_extent = 16. / 9.;
const double ccx_coeff = 1. / 6.;
// General result for z rotations, Eq. 28 in arXiv 1809.00128
inline double u1_extent(double lambda) {
// Shift parameter into +- 2 Pi
uint_t shift_factor = std::floor(std::abs(lambda) / (2 * M_PI));
if (shift_factor != 0) {
if (lambda < 0)
lambda += shift_factor * (2 * M_PI);
else
lambda -= shift_factor * (2 * M_PI);
}

// Shift parameter into +- Pi
if (lambda > M_PI)
lambda -= 2 * M_PI;
else if (lambda < -1 * M_PI)
lambda += 2 * M_PI;
double angle = std::abs(lambda);
bool s_z_quadrant = (angle > M_PI / 2);
if (s_z_quadrant)
angle = angle - M_PI / 2;
angle /= 2;
return std::pow(std::cos(angle) + tan_pi_over_8 * std::sin(angle), 2.);
}

} // namespace CHSimulator

#endif
Loading

0 comments on commit 64311f4

Please sign in to comment.