def test_reverse_qargs(self):
        """Test reverse_qargs method"""
        circ1 = QFT(5)
        circ2 = circ1.reverse_bits()

        state1 = Statevector.from_instruction(circ1)
        state2 = Statevector.from_instruction(circ2)
        self.assertEqual(state1.reverse_qargs(), state2)
 def test_drawings(self):
     """Test draw method"""
     qc1 = QFT(5)
     sv = Statevector.from_instruction(qc1)
     with self.subTest(msg="str(statevector)"):
         str(sv)
     for drawtype in ["repr", "text", "latex", "latex_source", "qsphere", "hinton", "bloch"]:
         with self.subTest(msg=f"draw('{drawtype}')"):
             sv.draw(drawtype)
    def test_global_phase(self):
        """Test global phase is handled correctly when evolving statevector."""

        qc = QuantumCircuit(1)
        qc.rz(0.5, 0)
        qc2 = transpile(qc, basis_gates=["p"])
        sv = Statevector.from_instruction(qc2)
        expected = np.array([0.96891242 - 0.24740396j, 0])
        self.assertEqual(float(qc2.global_phase), 2 * np.pi - 0.25)
        self.assertEqual(sv, Statevector(expected))
Beispiel #4
0
 def test_drawings(self):
     """Test draw method"""
     qc1 = QFT(5)
     sv = Statevector.from_instruction(qc1)
     with self.subTest(msg='str(statevector)'):
         str(sv)
     for drawtype in [
             'text', 'latex', 'latex_source', 'qsphere', 'hinton', 'bloch'
     ]:
         with self.subTest(msg=f"draw('{drawtype}')"):
             sv.draw(drawtype)
Beispiel #5
0
    def test_from_circuit(self):
        """Test initialization from a circuit."""
        # random unitaries
        u0 = random_unitary(2).data
        u1 = random_unitary(2).data
        # add to circuit
        qr = QuantumRegister(2)
        circ = QuantumCircuit(qr)
        circ.unitary(u0, [qr[0]])
        circ.unitary(u1, [qr[1]])
        target = Statevector(np.kron(u1, u0).dot([1, 0, 0, 0]))
        vec = Statevector.from_instruction(circ)
        self.assertEqual(vec, target)

        # Test tensor product of 1-qubit gates
        circuit = QuantumCircuit(3)
        circuit.h(0)
        circuit.x(1)
        circuit.ry(np.pi / 2, 2)
        target = Statevector.from_label('000').evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test decomposition of Controlled-u1 gate
        lam = np.pi / 4
        circuit = QuantumCircuit(2)
        circuit.h(0)
        circuit.h(1)
        circuit.cu1(lam, 0, 1)
        target = Statevector.from_label('00').evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test decomposition of controlled-H gate
        circuit = QuantumCircuit(2)
        circ.x(0)
        circuit.ch(0, 1)
        target = Statevector.from_label('00').evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)
 def test_from_circuit(self):
     """Test initialization from a circuit."""
     # random unitaries
     u0 = random_unitary(2).data
     u1 = random_unitary(2).data
     # add to circuit
     qr = QuantumRegister(2)
     circ = QuantumCircuit(qr)
     circ.unitary(u0, [qr[0]])
     circ.unitary(u1, [qr[1]])
     target = Statevector(np.kron(u1, u0).dot([1, 0, 0, 0]))
     vec = Statevector.from_instruction(circ)
     self.assertEqual(vec, target)
 def test_from_instruction(self):
     """Test initialization from an instruction."""
     target = np.dot(HGate().to_matrix(), [1, 0])
     vec = Statevector.from_instruction(HGate()).data
     global_phase_equivalent = matrix_equal(vec, target, ignore_phase=True)
     self.assertTrue(global_phase_equivalent)
    def test_from_circuit(self):
        """Test initialization from a circuit."""
        # random unitaries
        u0 = random_unitary(2).data
        u1 = random_unitary(2).data
        # add to circuit
        qr = QuantumRegister(2)
        circ = QuantumCircuit(qr)
        circ.unitary(u0, [qr[0]])
        circ.unitary(u1, [qr[1]])
        target = Statevector(np.kron(u1, u0).dot([1, 0, 0, 0]))
        vec = Statevector.from_instruction(circ)
        self.assertEqual(vec, target)

        # Test tensor product of 1-qubit gates
        circuit = QuantumCircuit(3)
        circuit.h(0)
        circuit.x(1)
        circuit.ry(np.pi / 2, 2)
        target = Statevector.from_label("000").evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test decomposition of Controlled-Phase gate
        lam = np.pi / 4
        circuit = QuantumCircuit(2)
        circuit.h(0)
        circuit.h(1)
        circuit.cp(lam, 0, 1)
        target = Statevector.from_label("00").evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test decomposition of controlled-H gate
        circuit = QuantumCircuit(2)
        circ.x(0)
        circuit.ch(0, 1)
        target = Statevector.from_label("00").evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test custom controlled gate
        qc = QuantumCircuit(2)
        qc.x(0)
        qc.h(1)
        gate = qc.to_gate()
        gate_ctrl = gate.control()

        circuit = QuantumCircuit(3)
        circuit.x(0)
        circuit.append(gate_ctrl, range(3))
        target = Statevector.from_label("000").evolve(Operator(circuit))
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test initialize instruction
        target = Statevector([1, 0, 0, 1j]) / np.sqrt(2)
        circuit = QuantumCircuit(2)
        circuit.initialize(target.data, [0, 1])
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)

        # Test reset instruction
        target = Statevector([1, 0])
        circuit = QuantumCircuit(1)
        circuit.h(0)
        circuit.reset(0)
        psi = Statevector.from_instruction(circuit)
        self.assertEqual(psi, target)