class QuadraticForm(num_result_qubits=None, quadratic=None, linear=None, offset=None, little_endian=True)[source]

Implements a quadratic form on binary variables encoded in qubit registers.

A quadratic form on binary variables is a quadratic function $$Q$$ acting on a binary variable of $$n$$ bits, $$x = x_0 ... x_{n-1}$$. For an integer matrix $$A$$, an integer vector $$b$$ and an integer $$c$$ the function can be written as

$Q(x) = x^T A x + x^T b + c$

If $$A$$, $$b$$ or $$c$$ contain scalar values, this circuit computes only an approximation of the quadratic form.

Provided with $$m$$ qubits to encode the value, this circuit computes $$Q(x) \mod 2^m$$ in [two’s complement](https://stackoverflow.com/questions/1049722/what-is-2s-complement) representation.

$|x\rangle_n |0\rangle_m \mapsto |x\rangle_n |(Q(x) + 2^m) \mod 2^m \rangle_m$

Since we use two’s complement e.g. the value of $$Q(x) = 3$$ requires 2 bits to represent the value and 1 bit for the sign: 3 = ‘011’ where the first 0 indicates a positive value. On the other hand, $$Q(x) = -3$$ would be -3 = ‘101’, where the first 1 indicates a negative value and 01 is the two’s complement of 3.

If the value of $$Q(x)$$ is too large to be represented with m qubits, the resulting bitstring is $$(Q(x) + 2^m) \mod 2^m)$$.

The implementation of this circuit is discussed in , Fig. 6.

References

: Gilliam et al., Grover Adaptive Search for Constrained Polynomial Binary Optimization.

arXiv:1912.04088

Parameters
• num_result_qubits (Optional[int]) – The number of qubits to encode the result. Called $$m$$ in the class documentation.

• quadratic (Union[ndarray, List[List[Union[float, ParameterExpression]]], None]) – A matrix containing the quadratic coefficients, $$A$$.

• linear (Union[ndarray, List[Union[float, ParameterExpression]], None]) – An array containing the linear coefficients, $$b$$.

• offset (Union[float, ParameterExpression, None]) – A constant offset, $$c$$.

• little_endian (bool) – Encode the result in little endianness.

Raises
• ValueError – If linear and quadratic have mismatching sizes.

• ValueError – If num_result_qubits is unspecified but cannot be determined because some values of the quadratic form are parameterized.

Attributes

 QuadraticForm.ancillas Returns a list of ancilla bits in the order that the registers were added. QuadraticForm.clbits Returns a list of classical bits in the order that the registers were added. QuadraticForm.data Return the circuit data (instructions and context). QuadraticForm.extension_lib QuadraticForm.global_phase Return the global phase of the circuit in radians. QuadraticForm.header QuadraticForm.instances QuadraticForm.n_qubits Deprecated, use num_qubits instead. QuadraticForm.num_ancillas Return the number of ancilla qubits. QuadraticForm.num_clbits Return number of classical bits. QuadraticForm.num_parameters Convenience function to get the number of parameter objects in the circuit. QuadraticForm.num_qubits Return number of qubits. QuadraticForm.parameters Convenience function to get the parameters defined in the parameter table. QuadraticForm.prefix QuadraticForm.qubits Returns a list of quantum bits in the order that the registers were added.

Methods

 QuadraticForm.AND(qr_variables, qb_target, …) Build a collective conjunction (AND) circuit in place using mct. QuadraticForm.OR(qr_variables, qb_target, …) Build a collective disjunction (OR) circuit in place using mct. Return indexed operation. Return number of operations in circuit. Add registers. QuadraticForm.append(instruction[, qargs, cargs]) Append one or more instructions to the end of the circuit, modifying the circuit in place. QuadraticForm.assign_parameters(param_dict) Assign parameters to new parameters or values. QuadraticForm.barrier(*qargs) Apply Barrier. QuadraticForm.bind_parameters(value_dict) Assign numeric parameters to values yielding a new circuit. QuadraticForm.cast(value, _type) Best effort to cast value to type. Converts several classical bit representations (such as indexes, range, etc.) into a list of classical bits. QuadraticForm.ccx(control_qubit1, …[, …]) Apply CCXGate. QuadraticForm.ch(control_qubit, target_qubit, *) Apply CHGate. Return the current number of instances of this class, useful for auto naming. Return the prefix to use for auto naming. QuadraticForm.cnot(control_qubit, …[, …]) Apply CXGate. Append rhs to self if self contains compatible registers. QuadraticForm.compose(other[, qubits, …]) Compose circuit with other circuit or instruction, optionally permuting wires. QuadraticForm.control([num_ctrl_qubits, …]) Control this circuit on num_ctrl_qubits qubits. QuadraticForm.copy([name]) Copy the circuit. Count each operation kind in the circuit. QuadraticForm.cp(theta, control_qubit, …) Apply CPhaseGate. QuadraticForm.crx(theta, control_qubit, …) Apply CRXGate. QuadraticForm.cry(theta, control_qubit, …) Apply CRYGate. QuadraticForm.crz(theta, control_qubit, …) Apply CRZGate. QuadraticForm.cswap(control_qubit, …[, …]) Apply CSwapGate. QuadraticForm.csx(control_qubit, target_qubit) Apply CSXGate. QuadraticForm.cu(theta, phi, lam, gamma, …) Apply CUGate. QuadraticForm.cu1(theta, control_qubit, …) Apply CU1Gate. QuadraticForm.cu3(theta, phi, lam, …[, …]) Apply CU3Gate. QuadraticForm.cx(control_qubit, target_qubit, *) Apply CXGate. QuadraticForm.cy(control_qubit, target_qubit, *) Apply CYGate. QuadraticForm.cz(control_qubit, target_qubit, *) Apply CZGate. QuadraticForm.dcx(qubit1, qubit2) Apply DCXGate. Call a decomposition pass on this circuit, to decompose one level (shallow decompose). Return circuit depth (i.e., length of critical path). QuadraticForm.diag_gate(diag, qubit) Deprecated version of QuantumCircuit.diagonal. QuadraticForm.diagonal(diag, qubit) Attach a diagonal gate to a circuit. QuadraticForm.draw([output, scale, …]) Draw the quantum circuit. Append QuantumCircuit to the right hand side if it contains compatible registers. QuadraticForm.fredkin(control_qubit, …[, …]) Apply CSwapGate. Take in a QASM file and generate a QuantumCircuit object. QuadraticForm.from_qasm_str(qasm_str) Take in a QASM string and generate a QuantumCircuit object. QuadraticForm.h(qubit, *[, q]) Apply HGate. QuadraticForm.hamiltonian(operator, time, qubits) Apply hamiltonian evolution to to qubits. QuadraticForm.has_register(register) Test if this circuit has the register r. QuadraticForm.i(qubit, *[, q]) Apply IGate. QuadraticForm.id(qubit, *[, q]) Apply IGate. QuadraticForm.iden(qubit, *[, q]) Deprecated identity gate. QuadraticForm.initialize(params, qubits) Apply initialize to circuit. Invert (take adjoint of) this circuit. QuadraticForm.iso(isometry, q_input, …[, …]) Attach an arbitrary isometry from m to n qubits to a circuit. QuadraticForm.isometry(isometry, q_input, …) Attach an arbitrary isometry from m to n qubits to a circuit. QuadraticForm.iswap(qubit1, qubit2) Apply iSwapGate. QuadraticForm.mcmt(gate, control_qubits, …) Apply a multi-control, multi-target using a generic gate. QuadraticForm.mcrx(theta, q_controls, q_target) Apply Multiple-Controlled X rotation gate QuadraticForm.mcry(theta, q_controls, …[, …]) Apply Multiple-Controlled Y rotation gate QuadraticForm.mcrz(lam, q_controls, q_target) Apply Multiple-Controlled Z rotation gate QuadraticForm.mct(control_qubits, target_qubit) Apply MCXGate. QuadraticForm.mcu1(lam, control_qubits, …) Apply MCU1Gate. QuadraticForm.mcx(control_qubits, target_qubit) Apply MCXGate. QuadraticForm.measure(qubit, cbit) Measure quantum bit into classical bit (tuples). QuadraticForm.measure_active([inplace]) Adds measurement to all non-idle qubits. QuadraticForm.measure_all([inplace]) Adds measurement to all qubits. DEPRECATED: use circuit.reverse_ops(). QuadraticForm.ms(theta, qubits) Apply MSGate. How many non-entangled subcircuits can the circuit be factored to. Return number of non-local gates (i.e. Computes the number of tensor factors in the unitary (quantum) part of the circuit only. Computes the number of tensor factors in the unitary (quantum) part of the circuit only. QuadraticForm.p(theta, qubit) Apply PhaseGate. QuadraticForm.power(power[, matrix_power]) Raise this circuit to the power of power. QuadraticForm.qasm([formatted, filename]) Return OpenQASM string. Converts several qubit representations (such as indexes, range, etc.) into a list of qubits. QuadraticForm.r(theta, phi, qubit, *[, q]) Apply RGate. QuadraticForm.rcccx(control_qubit1, …) Apply RC3XGate. QuadraticForm.rccx(control_qubit1, …) Apply RCCXGate. Removes final measurement on all qubits if they are present. Repeat this circuit reps times. Get the number of required result qubits. Reset q. Return a circuit with the opposite order of wires. Reverse the circuit by reversing the order of instructions. QuadraticForm.rx(theta, qubit, *[, label, q]) Apply RXGate. QuadraticForm.rxx(theta, qubit1, qubit2) Apply RXXGate. QuadraticForm.ry(theta, qubit, *[, label, q]) Apply RYGate. QuadraticForm.ryy(theta, qubit1, qubit2) Apply RYYGate. QuadraticForm.rz(phi, qubit, *[, q]) Apply RZGate. QuadraticForm.rzx(theta, qubit1, qubit2) Apply RZXGate. QuadraticForm.rzz(theta, qubit1, qubit2) Apply RZZGate. QuadraticForm.s(qubit, *[, q]) Apply SGate. QuadraticForm.sdg(qubit, *[, q]) Apply SdgGate. Returns total number of gate operations in circuit. QuadraticForm.snapshot(label[, …]) Take a statevector snapshot of the internal simulator representation. Take a density matrix snapshot of simulator state. Take a snapshot of expectation value of an Operator. Take a probability snapshot of the simulator state. Take a stabilizer snapshot of the simulator state. Take a statevector snapshot of the simulator state. QuadraticForm.squ(unitary_matrix, qubit[, …]) Decompose an arbitrary 2*2 unitary into three rotation gates. QuadraticForm.swap(qubit1, qubit2) Apply SwapGate. QuadraticForm.sx(qubit) Apply SXGate. QuadraticForm.sxdg(qubit) Apply SXdgGate. QuadraticForm.t(qubit, *[, q]) Apply TGate. QuadraticForm.tdg(qubit, *[, q]) Apply TdgGate. QuadraticForm.to_gate([parameter_map, label]) Create a Gate out of this circuit. QuadraticForm.to_instruction([parameter_map]) Create an Instruction out of this circuit. QuadraticForm.toffoli(control_qubit1, …[, …]) Apply CCXGate. QuadraticForm.u(theta, phi, lam, qubit) Apply UGate. QuadraticForm.u1(theta, qubit, *[, q]) Apply U1Gate. QuadraticForm.u2(phi, lam, qubit, *[, q]) Apply U2Gate. QuadraticForm.u3(theta, phi, lam, qubit, *) Apply U3Gate. QuadraticForm.uc(gate_list, q_controls, q_target) Attach a uniformly controlled gates (also called multiplexed gates) to a circuit. QuadraticForm.ucg(angle_list, q_controls, …) Deprecated version of uc. QuadraticForm.ucrx(angle_list, q_controls, …) Attach a uniformly controlled (also called multiplexed) Rx rotation gate to a circuit. QuadraticForm.ucry(angle_list, q_controls, …) Attach a uniformly controlled (also called multiplexed) Ry rotation gate to a circuit. QuadraticForm.ucrz(angle_list, q_controls, …) Attach a uniformly controlled (also called multiplexed gates) Rz rotation gate to a circuit. QuadraticForm.ucx(angle_list, q_controls, …) Deprecated version of ucrx. QuadraticForm.ucy(angle_list, q_controls, …) Deprecated version of ucry. QuadraticForm.ucz(angle_list, q_controls, …) Deprecated version of ucrz. QuadraticForm.unitary(obj, qubits[, label]) Apply unitary gate to q. Return number of qubits plus clbits in circuit. QuadraticForm.x(qubit, *[, label, ctrl_state, q]) Apply XGate. QuadraticForm.y(qubit, *[, q]) Apply YGate. QuadraticForm.z(qubit, *[, q]) Apply ZGate. Return indexed operation. Return number of operations in circuit.