Skip to content

Commit

Permalink
CR Hamiltonian experiment without cr_gate (#794)
Browse files Browse the repository at this point in the history
* Bug fix; instantiate CR Ham expr without cr_gate

* fix another bug with extra unittest
  • Loading branch information
nkanazawa1989 authored May 9, 2022
1 parent e20cef2 commit 2119d72
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
17 changes: 8 additions & 9 deletions qiskit_experiments/library/characterization/cr_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,21 @@ def __init__(
Raises:
QiskitError: When ``qubits`` length is not 2.
"""
super().__init__(qubits, analysis=CrossResonanceHamiltonianAnalysis(), backend=backend)
# backend parameters required to run this experiment
# random values are populated here but these are immediately updated after backend is set
# this is to keep capability of generating circuits just for checking
self._dt = 1
self._cr_channel = 0
self._granularity = 1
self._cr_gate = cr_gate

if len(qubits) != 2:
raise QiskitError(
"Length of qubits is not 2. Please provide index for control and target qubit."
)

super().__init__(qubits, analysis=CrossResonanceHamiltonianAnalysis(), backend=backend)
self.set_experiment_options(flat_top_widths=flat_top_widths, **kwargs)
self._cr_gate = cr_gate

# backend parameters required to run this experiment
# random values are populated here but these are immediately updated after backend is set
# this is to keep capability of generating circuits just for checking
self._dt = 1
self._cr_channel = 0
self._granularity = 1

@classmethod
def _default_experiment_options(cls) -> Options:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Instantiating the :class:`.CrossResonanceHamiltonian` experiment without ``cr_gate``
and with ``backend`` has been raising AttributeError. The bug has been fixed.
22 changes: 22 additions & 0 deletions test/test_cross_resonance_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ def test_circuit_generation_no_backend(self):
# Not raise an error
expr.circuits()

def test_instance_with_backend_without_cr_gate(self):
"""Calling set backend method without setting cr gate."""
backend = FakeBogota()
setattr(
backend.configuration(),
"timing_constraints",
{"granularity": 16},
)

# not raise an error
exp = cr_hamiltonian.CrossResonanceHamiltonian(
qubits=(0, 1),
flat_top_widths=[1000],
backend=backend,
)
ref_config = backend.configuration()
self.assertEqual(exp._dt, ref_config.dt)

# These properties are set when cr_gate is not provided
self.assertEqual(exp._cr_channel, ref_config.control((0, 1))[0].index)
self.assertEqual(exp._granularity, ref_config.timing_constraints["granularity"])

@data(
[1e6, 2e6, 1e3, -3e6, -2e6, 1e4],
[-1e6, -2e6, 1e3, 3e6, 2e6, 1e4],
Expand Down

0 comments on commit 2119d72

Please sign in to comment.