Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add save_expval to extended_stabilizer #2161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions qiskit_aer/backends/aer_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ class AerSimulator(AerBackend):
"roerror",
"save_statevector",
"reset",
"save_expval",
"save_expval_var",
"delay",
]
),
Expand Down
11 changes: 9 additions & 2 deletions src/simulators/extended_stabilizer/ch_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,15 @@ double Runner::norm_estimation(uint_t n_samples, uint_t repetitions,
std::vector<uint_t> adiag_2(n_samples, 0ULL);
std::vector<std::vector<uint_t>> a(n_samples,
std::vector<uint_t>(n_qubits_, 0ULL));
std::vector<AER::RngEngine> rngs(num_threads_);
#pragma omp parallel if (num_threads_ > 1) num_threads(num_threads_)
{
int tid = omp_get_thread_num();
// make rng for each threads
if (tid == 0)
rngs[0] = rng;
else
rngs[tid].set_seed(rng.initial_seed() + tid);
#ifdef _WIN32
#pragma omp for
#else
Expand All @@ -421,13 +428,13 @@ double Runner::norm_estimation(uint_t n_samples, uint_t repetitions,
for (int_t l = 0; l < NSAMPLES; l++) {
for (int_t i = 0; i < NQUBITS; i++) {
for (int_t j = i; j < NQUBITS; j++) {
if (rng.rand() < 0.5) {
if (rngs[tid].rand() < 0.5) {
a[l][i] |= (1ULL << j);
a[l][j] |= (1ULL << i);
}
}
adiag_1[l] |= (a[l][i] & (1ULL << i));
if (rng.rand() < 0.5) {
if (rngs[tid].rand() < 0.5) {
adiag_2[l] |= (1ULL << i);
}
}
Expand Down
42 changes: 31 additions & 11 deletions src/simulators/extended_stabilizer/extended_stabilizer_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const Operations::OpSet StateOpSet(
Operations::OpType::bfunc,
Operations::OpType::qerror_loc,
Operations::OpType::save_statevec,
}, // Operations::OpType::save_expval, Operations::OpType::save_expval_var},
Operations::OpType::save_expval,
Operations::OpType::save_expval_var,
},
// Gates
{"CX", "u0", "u1", "p", "cx", "cz", "swap", "id",
"x", "y", "z", "h", "s", "sdg", "sx", "sxdg",
Expand Down Expand Up @@ -334,7 +336,8 @@ bool State::check_measurement_opt(InputIterator first,
if (op->type == Operations::OpType::measure ||
op->type == Operations::OpType::bfunc ||
op->type == Operations::OpType::save_statevec ||
op->type == Operations::OpType::save_expval) {
op->type == Operations::OpType::save_expval ||
op->type == Operations::OpType::save_expval_var) {
return false;
}
}
Expand Down Expand Up @@ -421,10 +424,10 @@ void State::apply_ops(InputIterator first, InputIterator last,
apply_save_statevector(op, result);
break;
// Disabled until can fix bug in expval
// case Operations::OpType::save_expval:
// case Operations::OpType::save_expval_var:
// apply_save_expval(op, result, rng);
// break;
case Operations::OpType::save_expval:
case Operations::OpType::save_expval_var:
apply_save_expval(op, result, rng);
break;
default:
throw std::invalid_argument("CH::State::apply_ops does not support "
"operations of the type \'" +
Expand Down Expand Up @@ -827,14 +830,15 @@ double State::expval_pauli(const reg_t &qubits, const std::string &pauli,
case 'I':
break;
case 'X':
paulis[0].X += (1ULL << qubits[pos]);
paulis[0].X |= (1ULL << qubits[pos]);
break;
case 'Y':
paulis[0].X += (1ULL << qubits[pos]);
paulis[0].Z += (1ULL << qubits[pos]);
paulis[0].X |= (1ULL << qubits[pos]);
paulis[0].Z |= (1ULL << qubits[pos]);
paulis[0].e += 1;
break;
case 'Z':
paulis[0].Z += (1ULL << qubits[pos]);
paulis[0].Z |= (1ULL << qubits[pos]);
break;
default: {
std::stringstream msg;
Expand All @@ -843,9 +847,25 @@ double State::expval_pauli(const reg_t &qubits, const std::string &pauli,
}
}
}
paulis[0].e = paulis[0].e % 4;
auto g_norm = state_cpy.norm_estimation(
norm_estimation_samples_, norm_estimation_repetitions_, paulis, rng);
return (2 * g_norm - phi_norm);
// std::cout << pauli << ", phi_norm = " << phi_norm << " gnorm = "<<g_norm
// << std::endl;

double t = 2 * g_norm - phi_norm;

if (BaseState::qreg_.get_num_states() == 1) {
// if all ops in circuit are cliffords, return -1, 0 or 1
if (t < -0.5)
return -1.0;
else if (t > 0.5)
return 1.0;
else
return 0.0;
} else {
return t;
}
}

double State::expval_pauli(const reg_t &qubits, const std::string &pauli) {
Expand Down
14 changes: 13 additions & 1 deletion test/terra/backends/aer_simulator/test_save_expval.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def _test_save_expval(self, circuit, oper, qubits, variance, **options):
"density_matrix",
"matrix_product_state",
"tensor_network",
"extended_stabilizer",
],
PAULI2,
)
Expand All @@ -102,6 +103,7 @@ def test_save_expval_bell_pauli(self, method, device, pauli):
"density_matrix",
"matrix_product_state",
"tensor_network",
"extended_stabilizer",
],
PAULI2,
)
Expand All @@ -121,6 +123,7 @@ def test_save_expval_stabilizer_pauli(self, method, device, pauli):
"density_matrix",
"matrix_product_state",
"tensor_network",
"extended_stabilizer",
],
PAULI2,
)
Expand All @@ -140,6 +143,7 @@ def test_save_expval_var_stabilizer_pauli(self, method, device, pauli):
"density_matrix",
"matrix_product_state",
"tensor_network",
"extended_stabilizer",
],
[[0, 1], [1, 0], [0, 2], [2, 0], [1, 2], [2, 1]],
)
Expand All @@ -158,6 +162,7 @@ def test_save_expval_stabilizer_hermitian(self, method, device, qubits):
"density_matrix",
"matrix_product_state",
"tensor_network",
"extended_stabilizer",
], # , 'extended_stabilizer'],
[[0, 1], [1, 0], [0, 2], [2, 0], [1, 2], [2, 1]],
)
Expand All @@ -169,7 +174,14 @@ def test_save_expval_var_stabilizer_hermitian(self, method, device, qubits):
self._test_save_expval(circ, oper, qubits, True, method=method, device=device)

@supported_methods(
["automatic", "statevector", "density_matrix", "matrix_product_state", "tensor_network"],
[
"automatic",
"statevector",
"density_matrix",
"matrix_product_state",
"tensor_network",
"extended_stabilizer",
],
PAULI2,
)
def test_save_expval_nonstabilizer_pauli(self, method, device, pauli):
Expand Down
Loading