fix example matrix and add hadamard gate example
Source Link
epelaaez
  • 2.5k
  • 1
  • 5
  • 27

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & -\beta \\ \beta & \alpha \end{bmatrix} $$$$ \begin{bmatrix} \alpha & \beta^* \\ \beta & -\alpha^* \end{bmatrix} $$

Here we choose to eliminate$a=\alpha$, $b=\beta^*$ (to make sure the phase factor since it’s not presentcomplex conjugate one is in the questiontop row; but you poseddon't need to worry about this if $\alpha, \beta \in \mathbb{R}$) and $\phi = \pi$. An example of this type of matrix is the Hadamard, that you can get by making $\alpha=\beta=\frac{1}{\sqrt{2}}$.

$$ H= \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} $$

I’m not sure how to do it in Q#, but to do this in Qiskit, define an np.array with the matrix you want. Then, create the matrix by running gate = UnitaryGate(your_matrix). Then, just append the gate as you normally would: qc.append(gate, [qubits]). However, you need to look out for Qiskit’s indexing. For more info on this, checkout this answer.

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & -\beta \\ \beta & \alpha \end{bmatrix} $$

Here we choose to eliminate the phase factor since it’s not present in the question you posed.

I’m not sure how to do it in Q#, but to do this in Qiskit, define an np.array with the matrix you want. Then, create the matrix by running gate = UnitaryGate(your_matrix). Then, just append the gate as you normally would: qc.append(gate, [qubits]). However, you need to look out for Qiskit’s indexing. For more info on this, checkout this answer.

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & \beta^* \\ \beta & -\alpha^* \end{bmatrix} $$

Here we choose $a=\alpha$, $b=\beta^*$ (to make sure the complex conjugate one is in the top row; but you don't need to worry about this if $\alpha, \beta \in \mathbb{R}$) and $\phi = \pi$. An example of this type of matrix is the Hadamard, that you can get by making $\alpha=\beta=\frac{1}{\sqrt{2}}$.

$$ H= \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} $$

I’m not sure how to do it in Q#, but to do this in Qiskit, define an np.array with the matrix you want. Then, create the matrix by running gate = UnitaryGate(your_matrix). Then, just append the gate as you normally would: qc.append(gate, [qubits]). However, you need to look out for Qiskit’s indexing. For more info on this, checkout this answer.

Post Undeleted by epelaaez
Post Deleted by epelaaez
added 357 characters in body; added 37 characters in body
Source Link
epelaaez
  • 2.5k
  • 1
  • 5
  • 27

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & -\beta \\ \beta & \alpha \end{bmatrix} $$

Here we choose to eliminate the phase factor since it’s not present in the question you posed.

I’m not sure how to do it in Q#, but to do this in Qiskit, define an np.array with the matrix you want. Then, create the matrix by running gate = UnitaryGate(your_matrix). Then, just append the gate as you normally would: qc.append(gate, [qubits]). However, you need to look out for Qiskit’s indexing. For more info on this, checkout this answer.

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & -\beta \\ \beta & \alpha \end{bmatrix} $$

Here we choose to eliminate the phase factor since it’s not present in the question you posed.

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & -\beta \\ \beta & \alpha \end{bmatrix} $$

Here we choose to eliminate the phase factor since it’s not present in the question you posed.

I’m not sure how to do it in Q#, but to do this in Qiskit, define an np.array with the matrix you want. Then, create the matrix by running gate = UnitaryGate(your_matrix). Then, just append the gate as you normally would: qc.append(gate, [qubits]). However, you need to look out for Qiskit’s indexing. For more info on this, checkout this answer.

Source Link
epelaaez
  • 2.5k
  • 1
  • 5
  • 27

First, note that the input (in vector form) will be $\begin{bmatrix} 1 \\ 0 \end{bmatrix}$. And you want the output to be $\begin{bmatrix} \alpha \\ \beta \end{bmatrix}$. Therefore, the matrix that represents the gate you want needs to have the vector you want as output on the first column.

To see what the second column needs to be, let’s look at the general expression of a unitary matrix. This is

$$ \begin{bmatrix} a & b \\ -e^{i \phi}b^* & e^{i \phi}a^* \end{bmatrix} $$

Therefore, plugging in the first column and solving for the elements of the second column, the form of the matrix you want is

$$ \begin{bmatrix} \alpha & -\beta \\ \beta & \alpha \end{bmatrix} $$

Here we choose to eliminate the phase factor since it’s not present in the question you posed.