Skip to content

Commit

Permalink
add noise test cases and remove debug codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Apr 18, 2019
1 parent 5db32bc commit f6412bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/simulators/qasm/basic_optimization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ oplist_t Fusion::aggregate(const oplist_t& original) const {
++applied_total;
}

std::cout << applied_total / static_cast<double> (original.size()) << std::endl;

if (applied_total / static_cast<double> (original.size()) < 0.25)
return original;

Expand Down
42 changes: 42 additions & 0 deletions test/terra/utils/qasm_simulator/qasm_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
from test.terra.utils import common
from qiskit import compile, QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.providers.aer import QasmSimulator
from qiskit.providers.aer.noise import NoiseModel
from test.terra.utils import ref_1q_clifford
from test.terra.utils import ref_2q_clifford
import json
from qiskit.providers.aer import noise as aer_noise
from qiskit.providers.aer.noise import NoiseModel
from qiskit.providers.aer.noise.errors import ReadoutError, depolarizing_error


class QasmFusionTests(common.QiskitAerTestCase):
Expand All @@ -40,20 +44,58 @@ def create_statevector_circuit(self):
circuit.measure(qr, cr)
return circuit

def noise_model(self):
readout_error = [0.01, 0.1]
depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)}
noise = NoiseModel()
readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]]
noise.add_all_qubit_readout_error(ReadoutError(readout))
for gate, (num_qubits, gate_error) in depolarizing.items():
noise.add_all_qubit_quantum_error(depolarizing_error(gate_error, num_qubits), gate)
return noise

def check_mat_exist(self, result):
fusion_gates = result.as_dict()['results'][0]['metadata']['fusion_verbose']
for gate in fusion_gates:
print (gate)

def test_clifford_no_fusion(self):
"""Test Fusion with clifford simulator"""
shots = 100
circuits = ref_2q_clifford.cx_gate_circuits_deterministic(final_measure=True)
qobj = compile(circuits, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(qobj, backend_options={'fusion_verbose': True}).result()
self.is_completed(result)

self.assertTrue('results' in result.as_dict(),
msg="results must exist in result")
self.assertTrue('metadata' in result.as_dict()['results'][0],
msg="metadata must exist in results[0]")
self.assertTrue('fusion_verbose' not in result.as_dict()['results'][0]['metadata'],
msg="fusion must not work for clifford")

def test_noise_fusion(self):
"""Test Fusion with noise model option"""
circuit = self.create_statevector_circuit()

shots = 100

noise_model = self.noise_model()

qobj = compile([circuit], self.SIMULATOR, shots=shots, seed=1, basis_gates=noise_model.basis_gates)

print (qobj.as_dict())

result = self.SIMULATOR.run(qobj, noise_model=noise_model, backend_options={'fusion_enable': True, 'fusion_verbose': True}).result()
self.is_completed(result)

self.assertTrue('results' in result.as_dict(),
msg="results must exist in result")
self.assertTrue('metadata' in result.as_dict()['results'][0],
msg="metadata must exist in results[0]")
self.assertTrue('fusion_verbose' in result.as_dict()['results'][0]['metadata'],
msg="verbose must work with noise")

def test_fusion_verbose(self):
"""Test Fusion with verbose option"""
circuit = self.create_statevector_circuit()
Expand Down

0 comments on commit f6412bf

Please sign in to comment.