Repository: PennyLaneAI/pennylane

Issue:
int to qml.BasisEmbedding
### Feature details

Currently, to use `BasisEmbedding` we have to pass a list with the bits [0,1,0,0] for example to put a PauliX gate in the second qubit. It would be nice to be able to pass an integer number like:

```python
qml.BasisEmbedding(4, wires = range(4))
```
This result would be the same that:

```python
qml.BasisEmbedding([0,1,0,0], wires = range(4))
```
because 0100 is the number 4 in binary (note that PennyLane uses the convention $|q_0,q_1,\dots,q_{N-1}\rangle$ where $q_0$ is the most significant bit).

This feature have a lot of applications, for example for qRAMs where we could do things like this one:

```python
for i in range(16):
      qml.BasisEmbedding(i, wires = range(4))
     ### some stuff here
```

### Implementation

I'm using this functions to translate the integer to the list:

```python
def list_d(num, n_wires):

    mods = []
    while num != 0 or len(mods) != n_wires:
        mods.insert(0, num % 2)
        num //= 2

    return mods
```

```
list_d(4,5)
>>> [0,0,1,0,0]
```

Probably incorporating this routine will be enough. However, some checks should be added because if num > 2^n_wires, the code will fail.


### How important would you say this feature is?

1: Not important. Would be nice to have.

### Additional information

Make sure that the order of the list is the same as Pennylane. 
There are places where it is written from top to bottom and vice versa.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#2601](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (43ab01a) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/a61e25a0454812b69f259b5e76be42fe4803737d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (a61e25a) will **decrease** coverage by `40.70%`.
> The diff coverage is `25.00%`.

```diff
@@             Coverage Diff             @@
##           master    #2601       +/-   ##
===========================================
- Coverage   99.58%   58.88%   -40.71%     
===========================================
  Files         243      243               
  Lines       19589    19592        +3     
===========================================
- Hits        19508    11536     -7972     
- Misses         81     8056     +7975     
```


| [Impacted Files](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/templates/embeddings/basis.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3RlbXBsYXRlcy9lbWJlZGRpbmdzL2Jhc2lzLnB5) | `84.84% <25.00%> (-15.16%)` | :arrow_down: |
| [pennylane/interfaces/jax.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2ludGVyZmFjZXMvamF4LnB5) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pennylane/interfaces/jax\_jit.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2ludGVyZmFjZXMvamF4X2ppdC5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pennylane/interfaces/tensorflow.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2ludGVyZmFjZXMvdGVuc29yZmxvdy5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pennylane/interfaces/tensorflow\_autograph.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2ludGVyZmFjZXMvdGVuc29yZmxvd19hdXRvZ3JhcGgucHk=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pennylane/devices/default\_qubit\_jax.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2RldmljZXMvZGVmYXVsdF9xdWJpdF9qYXgucHk=) | `0.00% <0.00%> (-96.67%)` | :arrow_down: |
| [pennylane/devices/default\_qubit\_tf.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2RldmljZXMvZGVmYXVsdF9xdWJpdF90Zi5weQ==) | `0.00% <0.00%> (-92.76%)` | :arrow_down: |
| [pennylane/qchem/openfermion\_obs.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3FjaGVtL29wZW5mZXJtaW9uX29icy5weQ==) | `7.26% <0.00%> (-92.74%)` | :arrow_down: |
| [pennylane/hf/hamiltonian.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2hmL2hhbWlsdG9uaWFuLnB5) | `8.13% <0.00%> (-91.87%)` | :arrow_down: |
| [pennylane/fourier/visualize.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2ZvdXJpZXIvdmlzdWFsaXplLnB5) | `9.21% <0.00%> (-90.79%)` | :arrow_down: |
| ... and [185 more](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Last update [a61e25a...43ab01a](https://codecov.io/gh/PennyLaneAI/pennylane/pull/2601?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).

KetpuntoG: Before merge @tal66 , there is a document called changelog-dev.md where we usually include the change description. You would have to add the short description and at the end include your name as an author of this release 😄

Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 4630e2225..1a65e5b7e 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -104,6 +104,9 @@
 * Sparse Hamiltonians representation has changed from COOrdinate (COO) to Compressed Sparse Row (CSR) format. The CSR representation is more performant for arithmetic operations and matrix vector products. This change decreases the `expval()` calculation time, for `qml.SparseHamiltonian`, specially for large workflows. Also, the CRS format consumes less memory for the `qml.SparseHamiltonian` storage.
 [(#2561)](https://github.com/PennyLaneAI/pennylane/pull/2561)
 
+* `BasisEmbedding` can accept an int as argument instead of a list of bits (optionally). Example: `qml.BasisEmbedding(4, wires = range(4))` is now equivalent to `qml.BasisEmbedding([0,1,0,0], wires = range(4))` (because 4=0b100). 
+  [(#2601)](https://github.com/PennyLaneAI/pennylane/pull/2601)
+
 <h3>Breaking changes</h3>
 
 * The `qml.queuing.Queue` class is now removed.
diff --git a/pennylane/templates/embeddings/basis.py b/pennylane/templates/embeddings/basis.py
index 09df0c603..fec97bf68 100644
--- a/pennylane/templates/embeddings/basis.py
+++ b/pennylane/templates/embeddings/basis.py
@@ -23,8 +23,8 @@ from pennylane.wires import Wires
 class BasisEmbedding(Operation):
     r"""Encodes :math:`n` binary features into a basis state of :math:`n` qubits.
 
-    For example, for ``features=np.array([0, 1, 0])``, the quantum system will be
-    prepared in state :math:`|010 \rangle`.
+    For example, for ``features=np.array([0, 1, 0])`` or ``features=2`` (binary 10), the
+    quantum system will be prepared in state :math:`|010 \rangle`.
 
     .. warning::
 
@@ -72,6 +72,10 @@ class BasisEmbedding(Operation):
 
     def __init__(self, features, wires, do_queue=True, id=None):
 
+        if isinstance(features, int):
+            bin_string = f"{features:b}".zfill(len(wires))
+            features = [1 if d == "1" else 0 for d in bin_string]
+
         wires = Wires(wires)
         shape = qml.math.shape(features)
 
@@ -80,7 +84,9 @@ class BasisEmbedding(Operation):
 
         n_features = shape[0]
         if n_features != len(wires):
-            raise ValueError(f"Features must be of length {len(wires)}; got length {n_features}.")
+            raise ValueError(
+                f"Features must be of length {len(wires)}; got length {n_features} (features={features})."
+            )
 
         features = list(qml.math.toarray(features))
 


Test patch:
diff --git a/tests/templates/test_embeddings/test_basis.py b/tests/templates/test_embeddings/test_basis.py
index 3d00d87db..f34f9ba83 100644
--- a/tests/templates/test_embeddings/test_basis.py
+++ b/tests/templates/test_embeddings/test_basis.py
@@ -76,7 +76,20 @@ class TestDecomposition:
 class TestInputs:
     """Test inputs and pre-processing."""
 
-    @pytest.mark.parametrize("x", [[0], [0, 1, 1]])
+    @pytest.mark.parametrize(
+        ("feat","wires","expected"),
+        [(7, range(3), [1, 1, 1]), (2, range(4), [0, 0, 1, 0]), (8, range(5), [0, 1, 0, 0, 0])],
+    )
+    def test_features_as_int_conversion(self, feat, wires, expected):
+        """checks conversion from features as int to a list of binary digits
+           with length = len(wires)"""
+
+        assert (
+            qml.BasisEmbedding(features=feat, wires=wires).hyperparameters["basis_state"]
+            == expected
+        )
+
+    @pytest.mark.parametrize("x", [[0], [0, 1, 1], 4])
     def test_wrong_input_bits_exception(self, x):
         """Checks exception if number of features is not same as number of qubits."""