Repository: Qiskit/qiskit

Issue:
`pulse.build()` raises an error when `default_alignment` is set to `AlignmentKind` instance
### Environment

- **Qiskit version**: main
- **Python version**: 3.9
- **Operating system**: windows


### What is happening?

`pulse.build()` raises an error when `default_alignment` is set to `AlignmentKind` instance.

### How can we reproduce the issue?

```python
from qiskit.pulse import build
from qiskit.pulse.transforms import AlignLeft

with build(default_alignment=AlignLeft()) as schblock:
  pass

### What should happen?

The builder should accept the `AlignmentKind` instance as valid input, and set it as the default alignment.

### Any suggestions?

This [line](https://github.com/Qiskit/qiskit/blob/8557369597c7c7f3a0abe3f8b07045752ec10061/qiskit/pulse/builder.py#L534) assumes string identifier input, and ignores the possibility of `AlignmentKind` instance input.


Discussion:

Resolving patch:
diff --git a/qiskit/pulse/builder.py b/qiskit/pulse/builder.py
--- a/qiskit/pulse/builder.py
+++ b/qiskit/pulse/builder.py
@@ -531,7 +531,10 @@ def __init__(
             self._context_stack.append(root_block)
 
         # Set default alignment context
-        alignment = _PulseBuilder.__alignment_kinds__.get(default_alignment, default_alignment)
+        if isinstance(default_alignment, AlignmentKind):  # AlignmentKind instance
+            alignment = default_alignment
+        else:  # str identifier
+            alignment = _PulseBuilder.__alignment_kinds__.get(default_alignment, default_alignment)
         if not isinstance(alignment, AlignmentKind):
             raise exceptions.PulseError(
                 f"Given `default_alignment` {repr(default_alignment)} is "


Test patch:
diff --git a/test/python/pulse/test_builder.py b/test/python/pulse/test_builder.py
--- a/test/python/pulse/test_builder.py
+++ b/test/python/pulse/test_builder.py
@@ -21,6 +21,7 @@
 from qiskit.pulse.transforms import target_qobj_transform
 from qiskit.providers.fake_provider import FakeOpenPulse2Q, Fake127QPulseV1
 from qiskit.pulse import library, instructions
+from qiskit.pulse.exceptions import PulseError
 from test import QiskitTestCase  # pylint: disable=wrong-import-order
 
 
@@ -106,6 +107,29 @@ def test_default_alignment_sequential(self):
 
         self.assertScheduleEqual(schedule, reference)
 
+    def test_default_alignment_alignmentkind_instance(self):
+        """Test default AlignmentKind instance"""
+        d0 = pulse.DriveChannel(0)
+        d1 = pulse.DriveChannel(0)
+
+        with pulse.build(default_alignment=pulse.transforms.AlignEquispaced(100)) as schedule:
+            pulse.delay(10, d0)
+            pulse.delay(20, d1)
+
+        with pulse.build() as reference:
+            with pulse.align_equispaced(100):
+                pulse.delay(10, d0)
+                pulse.delay(20, d1)
+
+        self.assertScheduleEqual(schedule, reference)
+
+    def test_unknown_string_identifier(self):
+        """Test that unknown string identifier raises an error"""
+
+        with self.assertRaises(PulseError):
+            with pulse.build(default_alignment="unknown") as _:
+                pass
+
 
 class TestContexts(TestBuilder):
     """Test builder contexts."""