Skip to content

Commit bc93481

Browse files
authored
[QI-NNN] Add reset gate to circuit parser (#163)
1 parent 9d9e85c commit bc93481

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

src/quantuminspire/qiskit/backend_qx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class QuantumInspireBackend(Backend): # type: ignore
5454
backend_name='qi_simulator',
5555
backend_version=quantum_inspire_version,
5656
n_qubits=26,
57-
basis_gates=['x', 'y', 'z', 'h', 'rx', 'ry', 'rz', 's', 'sdg', 't', 'tdg', 'cx', 'ccx', 'p',
58-
'id', 'swap', 'cz', 'snapshot', 'delay', 'barrier'],
57+
basis_gates=['x', 'y', 'z', 'h', 'rx', 'ry', 'rz', 's', 'sdg', 't', 'tdg', 'cx', 'ccx', 'p', 'u',
58+
'id', 'swap', 'cz', 'snapshot', 'delay', 'barrier', 'reset'],
5959
gates=[GateConfig(name='NotUsed', parameters=['NaN'], qasm_def='NaN')],
6060
local=False,
6161
simulator=True,

src/quantuminspire/qiskit/circuit_parser.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from io import StringIO
1919
from typing import Optional, Tuple, List
2020

21-
import numpy as np
22-
2321
from qiskit.qobj import QasmQobjInstruction
2422
from quantuminspire.exceptions import ApiError
2523
from quantuminspire.qiskit.measurements import Measurements
@@ -37,7 +35,6 @@ def __init__(self, basis_gates: List[str], measurements: Measurements, full_stat
3735
self.basis_gates = basis_gates.copy()
3836
if len(self.basis_gates) > 0:
3937
self.basis_gates.append("measure")
40-
self.basis_gates.append("u")
4138
self.bfunc_instructions: List[QasmQobjInstruction] = []
4239
self.full_state_projection = full_state_projection
4340
self.measurements = measurements
@@ -566,6 +563,15 @@ def _c_barrier(stream: StringIO, instruction: QasmQobjInstruction, binary_contro
566563
567564
"""
568565

566+
def _reset(self, stream: StringIO, instruction: QasmQobjInstruction) -> None:
567+
""" Translates the reset element.
568+
569+
:param stream: The string-io stream to where the resulting cQASM is written.
570+
:param instruction: The Qiskit instruction to translate to cQASM.
571+
572+
"""
573+
stream.write('prep_z q[{0}]\n'.format(*instruction.qubits))
574+
569575
@staticmethod
570576
def _delay(stream: StringIO, instruction: QasmQobjInstruction) -> None:
571577
""" Translates the delay element for a qubit. In cQASM wait parameter is int and the unit is hardware cycles.

src/quantuminspire/qiskit/quantum_inspire_provider.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _adjust_backend_configuration(config: QasmBackendConfiguration, backend: Dic
9292
config.basis_gates = []
9393
for keys in backend['allowed_operations']:
9494
if keys in ['single_gates', 'parameterized_single_gates', 'dual_gates',
95-
'parameterized_dual_gates', 'triple_gates', 'wait', 'barrier']:
95+
'parameterized_dual_gates', 'triple_gates', 'wait', 'barrier', 'prep']:
9696
for gate in backend['allowed_operations'][keys]:
9797
if gate in ['x', 'y', 'z', 'h', 's', 't', 'rx', 'ry', 'rz', 'swap', 'cz', 'barrier']:
9898
config.basis_gates += [gate]
@@ -108,7 +108,10 @@ def _adjust_backend_configuration(config: QasmBackendConfiguration, backend: Dic
108108
config.basis_gates += ['cx']
109109
elif gate == 'toffoli':
110110
config.basis_gates += ['ccx']
111+
elif gate == 'prep' or gate == 'prep_z':
112+
config.basis_gates += ['reset']
111113
if 'rz' in config.basis_gates and 'ry' in config.basis_gates:
114+
config.basis_gates += ['u']
112115
config.basis_gates += ['p']
113116

114117
config.simulator = not backend['is_hardware_backend']

src/tests/quantuminspire/qiskit/test_backend_qx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def test_backend_default_configuration(self):
8686
backend_name='qi_simulator',
8787
backend_version=quantum_inspire_version,
8888
n_qubits=26,
89-
basis_gates=['x', 'y', 'z', 'h', 'rx', 'ry', 'rz', 's', 'sdg', 't', 'tdg', 'cx', 'ccx', 'p',
90-
'id', 'swap', 'cz', 'snapshot', 'delay', 'barrier'],
89+
basis_gates=['x', 'y', 'z', 'h', 'rx', 'ry', 'rz', 's', 'sdg', 't', 'tdg', 'cx', 'ccx', 'p', 'u',
90+
'id', 'swap', 'cz', 'snapshot', 'delay', 'barrier', 'reset'],
9191
gates=[GateConfig(name='NotUsed', parameters=['NaN'], qasm_def='NaN')],
9292
conditional=True,
9393
simulator=True,

src/tests/quantuminspire/qiskit/test_circuit_parser.py

+22
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,28 @@ def test_generate_cqasm_correct_output_delay_units_in_s(self):
270270
self.assertTrue('wait q[2], 1\n' in result)
271271
self.assertTrue('wait q[3], 1\n' in result)
272272

273+
def test_generate_cqasm_correct_output_reset_qubit(self):
274+
q1 = QuantumRegister(1, "q1")
275+
q2 = QuantumRegister(2, "q2")
276+
c1 = ClassicalRegister(1, "c1")
277+
c2 = ClassicalRegister(2, "c2")
278+
qc = QuantumCircuit(q1, q2, c1, c2, name="test")
279+
280+
qc.reset(q1)
281+
result = self._generate_cqasm_from_circuit(qc)
282+
self.assertTrue('prep_z q[0]\n' in result)
283+
284+
def test_generate_cqasm_correct_output_reset_qubits(self):
285+
q1 = QuantumRegister(1, "q1")
286+
q2 = QuantumRegister(2, "q2")
287+
c1 = ClassicalRegister(1, "c1")
288+
c2 = ClassicalRegister(2, "c2")
289+
qc = QuantumCircuit(q1, q2, c1, c2, name="test")
290+
291+
qc.reset(q2)
292+
result = self._generate_cqasm_from_circuit(qc)
293+
self.assertTrue('prep_z q[1]\nprep_z q[2]\n' in result)
294+
273295
def test_generate_cqasm_correct_output_identity(self):
274296
qc = QuantumCircuit(2, 2)
275297
qc.i(0)

src/tests/quantuminspire/qiskit/test_quantum_inspire_provider.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class TestQuantumInspireProvider(unittest.TestCase):
5656
'allowed_operations': {
5757
'measure': ['measure_z', 'measure_x'],
5858
'measure_all': ['measure_all'],
59+
'prep': ['prep_z'],
5960
'parameterized_single_gates': ['rx', 'ry', 'rz'],
6061
'single_gates': ['x', 'y', 'z', 'h', 'i', 't', 'tdag', 's', 'sdag'],
6162
'dual_gates': ['cz', 'cr', 'cnot', 'swap'],
@@ -127,8 +128,8 @@ def test_simulator_backend(self):
127128
self.assertTrue(backend.configuration().multiple_measurements)
128129
self.assertFalse(backend.configuration().parallel_computing)
129130
self.assertEqual(backend.configuration().basis_gates, ['x', 'y', 'z', 'h', 'rx', 'ry', 'rz', 's', 'sdg',
130-
't', 'tdg', 'cx', 'ccx', 'p', 'id',
131-
'swap', 'cz', 'snapshot', 'delay', 'barrier'])
131+
't', 'tdg', 'cx', 'ccx', 'p', 'u', 'id',
132+
'swap', 'cz', 'snapshot', 'delay', 'barrier', 'reset'])
132133

133134
def test_hardware_backend(self):
134135
with mock.patch('quantuminspire.qiskit.quantum_inspire_provider.QuantumInspireAPI') as api:
@@ -177,9 +178,10 @@ def test_hardware_backend2(self):
177178
self.assertFalse(backend.configuration().conditional)
178179
self.assertTrue(backend.configuration().multiple_measurements)
179180
self.assertFalse(backend.configuration().parallel_computing)
180-
self.assertEqual(backend.configuration().basis_gates, ['rx', 'ry', 'rz', 'x', 'y', 'z', 'h', 'id', 't',
181+
self.assertEqual(backend.configuration().basis_gates, ['reset', 'rx', 'ry', 'rz', 'x', 'y', 'z',
182+
'h', 'id', 't',
181183
'tdg', 's', 'sdg', 'cz', 'cx', 'swap',
182-
'ccx', 'barrier', 'delay', 'p'])
184+
'ccx', 'barrier', 'delay', 'u', 'p'])
183185

184186
def test_set_authentication_details(self):
185187
with mock.patch('quantuminspire.qiskit.quantum_inspire_provider.QuantumInspireAPI') as api:

0 commit comments

Comments
 (0)